Findbugs - Method ignores exceptional return value
Asked Answered
D

3

6

I am getting below Findbugs error for my below code. please let me know what needs to do for this?

Code:

public void myMethod(Key key, long timestampMillis) {
        File file = createFile(key, timestampMillis);
        boolean deleted = file.delete();
    }

<<Package/classname>> ignores exceptional return value of java.io.File.delete() 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.

Deleterious answered 20/7, 2011 at 5:5 Comment(0)
B
7

It's just letting you know that you're getting the output of file.delete() and then throwing it away. If you need to know if the delete succeeded, then do something with the deleted variable, otherwise your code is fine.

Bessette answered 20/7, 2011 at 5:10 Comment(2)
As you can see hereLibre
Well the code is not completely fine as you say. As pointed above you get Exceptional return value of java.io.File.delete() if you don't have a boolean variable assigned. But if you do have a boolean variable and don't use it, you get a Dead store to local variable FindBugs Bug.Gabriella
S
2

The annotation that is recommended to be used is:

@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
Sallet answered 19/11, 2018 at 12:54 Comment(0)
K
1

You can add to the method header

@SuppressWarnings({"ResultOfMethodCallIgnored"})

or in Android Studio press

Alt+Enter

And click

SuppressWarnings("ResultOfMethodCallIgnored") 

It's also working when posting as prefix before a line of code:

@SuppressWarnings("unchecked") (Unchecked)someUncheckedLineOfCode.getObject();
Keavy answered 14/7, 2016 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.