Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive in NoWhiteSpaceAfter #12345

Closed
DrVanScott opened this issue Oct 26, 2022 · 7 comments · Fixed by #12372
Closed

False positive in NoWhiteSpaceAfter #12345

DrVanScott opened this issue Oct 26, 2022 · 7 comments · Fixed by #12372

Comments

@DrVanScott
Copy link

DrVanScott commented Oct 26, 2022

hi,

starting with 8.45 8.44, checkstyle generates a false positive for this code

public class Demo {

    Object o;

    void some() {
        Object oo = new Object[4];
        this.o = ((Object[]) oo)[1];
    }
}

This leads to message like "There is a whitespace after 'o'"

<?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">
  <module name="TreeWalker">
    <module name="NoWhitespaceAfter">
    </module>
  </module>
</module>

produces this output using commandline / latest release 10.3.4

Starting audit...
[ERROR] ...\Demo.java:7:33: 'o' is followed by whitespace. [NoWhitespaceAfter]
Audit done.
Checkstyle ends with 1 errors.
@DrVanScott
Copy link
Author

Hi, don't really now all that information. I use Eclipse to verify my code. What else do you need? It depends on the above example, on rule "NoWhitespaceAfter" and on a version >= 8.44. Should be easy to reproduce.

@romani
Copy link
Member

romani commented Oct 26, 2022

Please reproduce it by our CLI as issue template demands

@DrVanScott
Copy link
Author

Above jave code (Demo.java) compiles without error or warning

Using this config:

<?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">
  <module name="TreeWalker">
    <module name="NoWhitespaceAfter">
    </module>
  </module>
</module>

produces this output using commandline / latest release 10.3.4

Starting audit...
[ERROR] ...\Demo.java:7:33: 'o' is followed by whitespace. [NoWhitespaceAfter]
Audit done.
Checkstyle ends with 1 errors.

@strkkk
Copy link
Member

strkkk commented Oct 26, 2022

I reproduced issue, it is clearly a bug.

@nrmancuso
Copy link
Member

Strange, it seems to be related to the combination of redundant this and casting to array type with access:


➜  src cat Test.java                                              
public class Test {

    Object o;

    void m1() {
        Object oo = new Object[4];
        this.o = ((Object[]) oo)[1]; // violation is here
    }

    void m2() {
        Object oo = new Object[4];
        o = ((Object[]) oo)[1];
    }

    void m3() {
        Object oo = new Object[4];
        this.o = new Object();
    }

    void m4() {
        Object oo = new Object[4];
        this.o = (int) oo;
    }

    void m5() {
        Object oo = new Object[4];
        this.o = ((int[][]) new Object())[1];
    }
}%                                                                                                             

 ➜  src cat config.xml                                             
<?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">
  <module name="TreeWalker">
    <module name="NoWhitespaceAfter">
    </module>
  </module>
</module>%  
                                                                                                    
➜  src javac Test.java                                            
➜  src java -jar checkstyle-10.3.4-all.jar -c config.xml Test.java
Starting audit...
[ERROR] /home/nick/IdeaProjects/checkstyle_tester/src/Test.java:7:33: 'o' is followed by whitespace. [NoWhitespaceAfter]
[ERROR] /home/nick/IdeaProjects/checkstyle_tester/src/Test.java:27:42: 'o' is followed by whitespace. [NoWhitespaceAfter]
Audit done.
Checkstyle ends with 2 errors.

strkkk added a commit to strkkk/checkstyle that referenced this issue Nov 5, 2022
strkkk added a commit to strkkk/checkstyle that referenced this issue Nov 5, 2022
@checkstyle checkstyle deleted a comment from nrmancuso Nov 5, 2022
@checkstyle checkstyle deleted a comment from DrVanScott Nov 5, 2022
@checkstyle checkstyle deleted a comment from rnveach Nov 5, 2022
strkkk added a commit to strkkk/checkstyle that referenced this issue Nov 13, 2022
strkkk added a commit to strkkk/checkstyle that referenced this issue Nov 13, 2022
strkkk added a commit to strkkk/checkstyle that referenced this issue Nov 24, 2022
strkkk added a commit to strkkk/checkstyle that referenced this issue Nov 25, 2022
@romani romani added this to the 10.5 milestone Nov 25, 2022
@strkkk
Copy link
Member

strkkk commented Nov 25, 2022

Fix is merged

@DrVanScott
Copy link
Author

Thanks for fixing this. I really like the ID of this issue, btw :-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants