如何处理Java代码中Checkstyle检测到的void方法return语句问题
Checkstyle ReturnCount 错误解决:void方法提前return引发的问题
我有两个Java方法,正在用Checkstyle做代码规范检测。之前把buildResultWithLabel()方法从buildResult()中提取出来,提取前这段代码处于循环内,用的是continue;语句;提取后不在循环里,改成了return;,现在收到Checkstyle报错:
ERROR: (coding) ReturnCount: Return count is 1 (max allowed for void methods/constructors/lambdas is 0).
我的代码如下:
public static Result buildResult(String localRepoPath, String authToken, String remoteRepoPath, String startRef, String endRef) throws IOException, GitAPIException { final Result result = new Result(); final GHRepository remoteRepo = createRemoteRepo(authToken, remoteRepoPath); final Set<RevCommit> commitsForRelease = getCommitsBetweenReferences(localRepoPath, startRef, endRef); commitsForRelease.removeAll(getIgnoredCommits(commitsForRelease)); final Set<Integer> processedIssueNumbers = new HashSet<>(); for (RevCommit commit : commitsForRelease) { CommitMessage commitMessage = new CommitMessage(commit.getFullMessage()); if (commitMessage.isRevert()) { System.out.println(commitMessage.getMessage()); commitMessage = new CommitMessage(commitMessage.getRevertedCommitMessage()); } buildResultWithLabel(remoteRepoPath, result, remoteRepo, commitsForRelease, processedIssueNumbers, commit, commitMessage); } return result; } private static void buildResultWithLabel(String remoteRepoPath, Result result, GHRepository remoteRepo, Set<RevCommit> commitsForRelease, Set<Integer> processedIssueNumbers, RevCommit commit, CommitMessage commitMessage) throws IOException { if (commitMessage.isIssueOrPull()) { final int issueNo = commitMessage.getIssueNumber(); if (processedIssueNumbers.contains(issueNo)) { return; } processedIssueNumbers.add(issueNo); final GHIssue issue = remoteRepo.getIssue(issueNo); if (issue.getState() != GHIssueState.CLOSED) { result.addWarning(String.format(MESSAGE_NOT_CLOSED, issueNo, issue.getTitle(), remoteRepoPath, issueNo)); } final String issueLabel = getIssueLabelFrom(issue); if (issueLabel.isEmpty()) { final String error = String.format(MESSAGE_NO_LABEL, issueNo, Arrays.stream(Constants.ISSUE_LABELS) .collect(Collectors.joining(SEPARATOR)), remoteRepoPath, issueNo); result.addError(error); } final List<GHLabel> releaseLabels = getAllIssueLabels(issue); if (releaseLabels.size() > 1) { final String error = String.format(MESSAGE_MORE_THAN_ONE_RELEASE_LABEL, issueNo, Arrays.stream(Constants.ISSUE_LABELS) .collect(Collectors.joining(SEPARATOR)), remoteRepoPath, issueNo); result.addError(error); } final Set<RevCommit> issueCommits = getCommitsForIssue(commitsForRelease, issueNo); final String authors = getAuthorsOf(issueCommits); final ReleaseNotesMessage releaseNotesMessage = new ReleaseNotesMessage(issue, authors); result.putReleaseNotesMessage(issueLabel, releaseNotesMessage); } else { // 提交消息不包含issue或PR编号的情况 final String commitShortMessage = commit.getShortMessage(); final String author = commit.getAuthorIdent().getName(); final ReleaseNotesMessage releaseNotesMessage = new ReleaseNotesMessage(commitShortMessage, author); result.putReleaseNotesMessage(Constants.MISCELLANEOUS_LABEL, releaseNotesMessage); } }
解决方案
1. 重构代码结构,移除提前return
把提前return的逻辑改成条件包裹后续代码,让方法只有默认的隐式return:
修改buildResultWithLabel方法中的对应片段:
// 原代码 if (processedIssueNumbers.contains(issueNo)) { return; } processedIssueNumbers.add(issueNo); // 后续逻辑代码...
替换为:
if (!processedIssueNumbers.contains(issueNo)) { processedIssueNumbers.add(issueNo); // 把原return之后的所有逻辑代码都移到这个if块内 final GHIssue issue = remoteRepo.getIssue(issueNo); if (issue.getState() != GHIssueState.CLOSED) { result.addWarning(String.format(MESSAGE_NOT_CLOSED, issueNo, issue.getTitle(), remoteRepoPath, issueNo)); } final String issueLabel = getIssueLabelFrom(issue); if (issueLabel.isEmpty()) { final String error = String.format(MESSAGE_NO_LABEL, issueNo, Arrays.stream(Constants.ISSUE_LABELS) .collect(Collectors.joining(SEPARATOR)), remoteRepoPath, issueNo); result.addError(error); } final List<GHLabel> releaseLabels = getAllIssueLabels(issue); if (releaseLabels.size() > 1) { final String error = String.format(MESSAGE_MORE_THAN_ONE_RELEASE_LABEL, issueNo, Arrays.stream(Constants.ISSUE_LABELS) .collect(Collectors.joining(SEPARATOR)), remoteRepoPath, issueNo); result.addError(error); } final Set<RevCommit> issueCommits = getCommitsForIssue(commitsForRelease, issueNo); final String authors = getAuthorsOf(issueCommits); final ReleaseNotesMessage releaseNotesMessage = new ReleaseNotesMessage(issue, authors); result.putReleaseNotesMessage(issueLabel, releaseNotesMessage); }
这样既保留了原有业务逻辑,又符合Checkstyle对void方法return次数的限制。
2. 调整Checkstyle配置,放宽规则
如果团队代码规范允许void方法使用提前return,可以修改Checkstyle的ReturnCount规则配置,设置void方法允许的最大return数:
<module name="ReturnCount"> <!-- 非void方法的最大return数,按需调整 --> <property name="max" value="2"/> <!-- 设置void方法允许的最大return数 --> <property name="maxForVoid" value="1"/> </module>
注意这种方式需要团队达成一致,避免代码风格混乱。
内容的提问来源于stack exchange,提问作者Rahul Khinchi
相关产品推荐
相关产品推荐

