Code Monkey home page Code Monkey logo

Comments (8)

rnveach avatar rnveach commented on July 28, 2024

Please add an example of a pattern as well.

I am good to add an input.

from checkstyle.

mahfouz72 avatar mahfouz72 commented on July 28, 2024

I don't get it. we can't declare a pattern with a magic number. The pattern is being matched only can you share example?

from checkstyle.

rnveach avatar rnveach commented on July 28, 2024

Sorry, patterns can't have numbers directly assigned to them. They are just type and variable name. I believe I was trying to say this issue didn't mention patterns at all, and patterns can have whens. This might be another issue, but did we try this check with when on patterns?

from checkstyle.

nrmancuso avatar nrmancuso commented on July 28, 2024

This check is fine on patterns with a when guard:

➜  src javac Test.java
➜  src cat Test.java  
public class Test {
    public static void main(String[] args) {

       Object o = new Object();

        if (o instanceof Integer i && i < 3) { // violation
            System.out.println("null");
        } else {
            System.out.println("not null");
        }

        switch (o) {
            case Integer i when i < 3: // violation
                System.out.println("null");
                break;
            default:
                System.out.println("not null");
        }
    }
}
➜  src cat config.xml 
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
        "http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
    <property name="charset" value="UTF-8"/>
    <module name="TreeWalker">
        <module name="MagicNumber">
        </module>
    </module>
</module>
➜  src java -jar checkstyle-10.17.0-all.jar -c config.xml Test.java
Starting audit...
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:6:43: '3' is a magic number. [MagicNumber]
[ERROR] /home/nick/IdeaProjects/tester/src/Test.java:13:37: '3' is a magic number. [MagicNumber]
Audit done.
Checkstyle ends with 2 errors.

@mahfouz72 do we have an issue for this one?

This check will not apply to unnamed patterns at all, since _ can never be referenced (therefore we cannot use it in a when guard where we would have some conditional).

from checkstyle.

mahfouz72 avatar mahfouz72 commented on July 28, 2024

do we have an issue for this one?

the things related to when only should be covered in the pattern matching for switch syntax tracker. this syntax is not related to unnamed patterns. But the list of the checks there doesn't have MagicNumberCheck. So, No we don't have an issue with this syntax for this check. do you think we should open one for an input file only (the behavior is correct)?


This check will not apply to unnamed patterns at all, since _ can never be referenced (therefore we cannot use it in a when guard where we would have some conditional).

then I think we can close this

from checkstyle.

rnveach avatar rnveach commented on July 28, 2024

the things related to when only should be covered in the pattern matching for switch syntax tracker

#14961

do you think we should open one for an input file only (the behavior is correct)?

I am good with this.

then I think we can close this

@nrmancuso

from checkstyle.

mahfouz72 avatar mahfouz72 commented on July 28, 2024

#15107

from checkstyle.

nrmancuso avatar nrmancuso commented on July 28, 2024

Approved for test inputs

from checkstyle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.