Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Result of method is ignored"- what does this imply?

Tags:

java

android

For some methods I get the warning. This is what it says when expanded. The following code (mkDirs()) gives the warning

if (!myDir.exists()) {
     myDir.mkdirs();
}

Reports any calls to specific methods where the result of that call is ignored. Both methods specified in the inspection's settings and methods annotated with org.jetbrains.annotations.Contract(pure=true) are checked. For many methods, ignoring the result is perfectly legitimate, but for some methods it is almost certainly an error. Examples of methods where ignoring the result of a call is likely to be an error include java.io.inputStream.read(), which returns the number of bytes actually read, any method on java.lang.String or java.math.BigInteger, as all of those methods are side-effect free and thus pointless if ignored.

What does it mean? How to to avoid it? How should it be addressed?

like image 205
suku Avatar asked Dec 10 '22 15:12

suku


2 Answers

It is possible to omit the warning using annotation

@SuppressWarnings("ResultOfMethodCallIgnored")
public void someMethod() {
  ...
  myDir.mkdirs();
  ...
}

If the directory exists, the mkdir() operation will return false, if it does not exist, it will be created (if you have the appropriate rights, of course), therefore, IMHO, the check via isExists() can be omitted.

However, as indicated above, if you are working with this directory, it is a good idea to make sure that it exists.

like image 76
Pivoter Avatar answered Dec 13 '22 04:12

Pivoter


This method suggests that you are creating directory with your specified path if directory is not already created then this function will create a new one for you.It is returning true/false weather directory exists or created.Perhaps in some situation due to low storage it not created at specified path and you are trying to write contents into file within that directory it will throw ioException.So you should utilise if condition

if(myDir.mkdirs())
{
   //execute what ever you want to do
}
else
{
   // show error message for any failure.
}
like image 27
Jay Shah Avatar answered Dec 13 '22 05:12

Jay Shah