[英]Sonar Issue "Return values should not be ignored when they contain the operation status code" getting for Java code
I am getting the Sonar Issue "Return values should not be ignored when they contain the operation status code" for below line of code.我收到以下代码行的声纳问题“返回值包含操作状态代码时不应被忽略”。
directory.delete();
Do something with the "boolean" value returned by "delete()" error I am getting.对我得到的“delete()”错误返回的“布尔”值做一些事情。 I tried to add condition like
我试图添加条件
if(!directory.delete()){
logger.error("failed to delete");
}
Sonar issue is getting fixed but it is affecting for code, please let me know, how to fix the issue.声纳问题正在修复,但它会影响代码,请告诉我如何解决该问题。
In this case you can use logger without side-effect for the code, for instance like that:在这种情况下,您可以对代码使用没有副作用的记录器,例如:
var retVal = directory.delete();
logger.info("entity " + reVal + " was deleted");
Sonar is telling you might better do something when the directory cannot be deleted. Sonar 告诉您,当目录无法删除时,您最好做点什么。 So leave the warning if you cannot think of some solution.
因此,如果您想不出一些解决方案,请留下警告。
Or log, but on trace level, which normally will not be logged, as opposed to error level logging.或记录,但在跟踪级别,通常不会记录,与错误级别记录相反。
if (!directory.delete()) {
logger.trace("failed to delete: " + directory);
}
An other solution: I would also advise to use the methods of Files
anyway.另一个解决方案:我也建议使用
Files
的方法。 There are some improvements.有一些改进。 Also in this case:
同样在这种情况下:
Path dir = directory.toPath();
try {
Files.delete(dir); // void, instead exceptions
if (Files.deleteIfExists(dir)) { // boolean: whether
// this call deleted the directory.
++deleteCount;
}
} catch (NoSuchFileException | DirectoryNotEmptyException
| IOException | SecurityException e {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.