Active/Severity | Name [expand / collapse] | Sort by: |
---|---|---|
This code generates a random signed integer and then computes the remainder of that value modulo another value. Since the random number can be negative, the result of the remainder operation can also be negative. Be sure this is intended, and strongly consider using the Random.nextInt(int) method instead.
Repository: findbugs
![]() ![]() |
||
This code computes a hashCode, and then computes the remainder of that value modulo another value. Since the hashCode can be negative, the result of the remainder operation can also be negative. Assuming you want to ensure that the result of your computation is nonnegative,
you may need to change your code.
If you know the divisor is a power of 2,
you can use a bitwise and operator instead (i.e., instead of
using
Repository: findbugs
![]() ![]() |
||
This code performs integer multiply and then converts the result to a long,
as in:
Repository: findbugs
![]() ![]() |
||
This operation compares two floating point values for equality.
Because floating point calculations may involve rounding,
calculated float and double values may not be accurate.
For values that must be precise, such as monetary values,
consider using a fixed-precision type such as BigDecimal.
For values that need not be precise, consider comparing for equality
within some range, for example:
Repository: findbugs
![]() ![]() |
||
The field is marked as transient, but the class isn't Serializable, so marking it as transient has absolutely no effect. This may be leftover marking from a previous version of the code in which the class was transient, or it may indicate a misunderstanding of how serialization works.
Repository: findbugs
![]() ![]() |
||
The code performs an unsigned right shift, whose result is then cast to a short or byte, which discards the upper bits of the result. Since the upper bits are discarded, there may be no difference between a signed and unsigned right shift (depending upon the size of the shift).
Repository: findbugs
![]() ![]() |
||
This method contains a useless control flow statement, where
control flow continues onto the same place regardless of whether or not
the branch is taken. For example,
this is caused by having an empty statement
block for an if (argv.length == 0) { // TODO: handle this case }
Repository: findbugs
![]() ![]() |
||
The entrySet() method is allowed to return a view of the underlying Map in which an
Repository: findbugs
![]() ![]() |
||
Returning The following code: public static Result[] getResults() { return null; // Non-Compliant } public static void main(String[] args) { Result[] results = getResults(); if (results != null) { // Nullity test required to prevent NPE for (Result result: results) { /* ... */ } } } should be refactored into: public static Result[] getResults() { return new Result[0]; // Compliant } public static void main(String[] args) { for (Result result: getResults()) { /* ... */ } } This rule also applies to collections: public static List<Result> getResults() { return null; // Non-Compliant } should be refactored into: public static List<Result> getResults() { return Collections.EMPTY_LIST; // Compliant }
Repository: squid
![]() ![]() |
||
Empty statements, i.e.
Examples: void doSomething() { ; // Non-Compliant - was used as a kind of TODO marker } System.out.println("Hello, world!");; // Non-Compliant - double ; Rarely, they are used on purpose, as the body of a loop: for (int i = 0; i < 3; System.out.println(i), i++); // Non-Compliant It is a bad practice to have side-effects outside of the loop body, and therefore that code should be refactored into: for (int i = 0; i < 3; i ++) { // Compliant System.out.println(i); }
Repository: squid
![]() ![]() |
||
From the official Oracle Javadoc: NOTE: The functionality of this Enumeration interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration. The following code: public class MyClass implements Enumeration { // Non-Compliant /* ... */ } should be refactored into: public class MyClass implements Iterator { // Compliant /* ... */ }
Repository: squid
![]() ![]() |
||
Deprecated
Inexperienced programmers sometimes confuse comparison concepts and use equals() to compare to null. This rule is deprecated, use squid:S1318 instead.
Repository: pmd
![]() ![]() |
||
Exceptions are meant to represent the application's state at which an error occurred. Making all fields final ensures that this state:
This will enable developers to quickly understand what went wrong. The following code: public class MyException extends Exception { private int status; // Non-Compliant public MyException(String message) { super(message); } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } } should be refactored into: public class MyException extends Exception { private final int status; // Compliant public MyException(String message, int status) { super(message); this.status = status; } public int getStatus() { return status; } }
Repository: squid
![]() ![]() |
||
When handling a caught exception, two mandatory informations should be logged:
Noncompliant Code Example// Noncompliant - exception is lost try { /* ... */ } catch (Exception e) { LOGGER.info("context"); } // Noncompliant - context is required try { /* ... */ } catch (Exception e) { LOGGER.info(e); } // Noncompliant - exception is lost (only message is preserved) try { /* ... */ } catch (Exception e) { LOGGER.info(e.getMessage()); } // Noncompliant - exception is lost try { /* ... */ } catch (Exception e) { throw new RuntimeException("context"); } Compliant Solutiontry { /* ... */ } catch (Exception e) { LOGGER.info("context", e); } try { /* ... */ } catch (Exception e) { throw new RuntimeException("context", e); } ExceptionsIt is allowed to let the exception propagate. try { /* ... */ } catch (RuntimeException e) { doSomething(); throw e; } catch (Exception e) { // Conversion into unchecked exception is also allowed throw new RuntimeException(e); }
int myInteger; try { myInteger = Integer.parseInt(myString); } catch (NumberFormatException e) { // It is perfectly acceptable to not handle "e" here myInteger = 0; }
Repository: squid
![]() ![]() |
||
Multiple catch blocks of the appropriate type should be used instead of catching a general exception, and then testing on the type. For example, following code: try { /* ... */ } catch (Exception e) { if(e instanceof IOException) { /* ... */ } // Non-Compliant if(e instanceof NullPointerException{ /* ... */ } // Non-Compliant } should be refactored into: try { /* ... */ } catch (IOException e) { /* ... */ } // Compliant } catch (NullPointerException e) { /* ... */ } // Compliant
Repository: squid
![]() ![]() |
||
Throwing an exception from within a finally block will mask any exception which was previously thrown in the The following code: try { /* some work which end up throwing an exception */ throw new IllegalArgumentException(); } finally { /* clean up */ throw new RuntimeException(); // Non-Compliant - will mask the IllegalArgumentException } should be refactored into: try { /* some work which end up throwing an exception */ throw new IllegalArgumentException(); } finally { /* clean up */ // Compliant }
Repository: squid
![]() ![]() |