In the following method i am getting the findbug violation RV_RETURN_VALUE_IGNORED_BAD_PRACTICE

StackOverflow https://stackoverflow.com/questions/20675328

  •  19-09-2022
  •  | 
  •  

Question

public File moveFileToUploadLoc(final File dir) throws Exception {
    File fileToMove = null;
    if (dir != null) {
        final File[] files = dir.listFiles();
        for (int i = 0; files != null && i < files.length; i++) {
        if (files[i].getName().endsWith("abcd")) {
                    fileToMove = files[i];

                    File destnFile = this.getFilePathFrmConfigRepo(this
                            .getFilePathKey());
                    fileToMove.renameTo(destinationFile);

                    break;
                }
            }
        }

        return fileToMove;
    }

it suggests that RV_RETURN_VALUE_IGNORED_BAD_PRACTICE

This method returns a value that is not checked. The return value should be checked since it can indicate an unusual or unexpected function execution. For example, the File.delete() method returns false if the file could not be successfully deleted (rather than throwing an Exception). If you don't check the result, you won't notice if the method invocation signals unexpected behavior by returning an atypical return value.

Was it helpful?

Solution

Reading the (now properly) quoted text, you see that the explanation is clear. For every function that returns a value, you should be using that function in some way.

Their example even specifically refers to a File method, delete(). In this case, findbugs is most likely referring to your call to File.renameTo().

You should be checking the return value of this function and handling error (false) accordingly.

OTHER TIPS

You are using File.renameTo() and you do ignore its return value. You should check it and if it's false, you should handle the situation, e.g., by throwing an exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top