目錄表

SonarLint | Return of boolean expressions should not be wrapped into an "if-then-else" statement (java:S1126)

Problem

private boolean assertFileContainString(Path filePath,String fileName, String expect) throws IOException {
	String content = FileUtil.getContent(new File(filePath.resolve(fileName).toString()));
 
	if(content.contains(expect)) // <code smell here!>
		return true;
	return false;
}
這個問題發生在回傳值為boolean,且你寫了不必要的if-then-else。

How to fix?

要解決這問題的方式,就是把不必要的if-then-else直接精簡成一行即可:

private boolean assertFileContainString(Path filePath,String fileName, String expect) throws IOException {
	String content = FileUtil.getContent(new File(filePath.resolve(fileName).toString()));
	return content.contains(expect);
}