Home > Blockchain >  Sonar Issue "Return values should not be ignored when they contain the operation status code&qu
Sonar Issue "Return values should not be ignored when they contain the operation status code&qu

Time:09-02

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. 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.

CodePudding user response:

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");

CodePudding user response:

Sonar is telling you might better do something when the directory cannot be deleted. 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. 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 {
    ...
}
  • Related