Skip to content

False positive: magicnumbers with bit shift expression #15950

@ghernadi

Description

@ghernadi

I have read check documentation: https://checkstyle.org/checks/coding/magicnumber.html
I have downloaded the latest cli from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

$ javac Test.java
$
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
    "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
    "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
  <property name="severity" value="error"/>
  <property name="fileExtensions" value="java, properties, xml"/>
  <module name="TreeWalker">
    <module name="MagicNumber">
       <property name="ignoreNumbers" value="-2,-1,0,1,2,100"/>
       <property name="ignoreHashCodeMethod" value="true"/>
       <property name="ignoreFieldDeclaration" value="true"/>
       <property name="severity" value="warning"/>
       <property name="id" value="checkstyle:magicnumber"/>
    </module>
  </module>
</module>
public class Test {
    public static final int BIT0 = 1;
    public static final int BIT1 = 1 << 1;
    public static final int BIT2 = 1 << 2;
    public static final int BIT3 = 1 << 3;
    public static final int BIT4 = 1 << 4;
}
$ java -jar checkstyle-10.20.1-all.jar -c config.xml Test.java 
Starting audit...
[WARN] /var/tmp/Test.java:5:41: '3' is a magic number. [checkstyle:magicnumber]
[WARN] /var/tmp/Test.java:6:41: '4' is a magic number. [checkstyle:magicnumber]
Audit done.

I would have expected bit-shift operators to also be excluded from this check, just like 2 * 2 * 2 would work without checkstyle complaining...

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedfalse positiveissues where check places violations on code, but should not

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions