Comments (8)
Please add an example of a pattern as well.
I am good to add an input.
from checkstyle.
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.
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 when
s. This might be another issue, but did we try this check with when
on patterns?
from checkstyle.
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.
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.
the things related to when only should be covered in the pattern matching for switch syntax tracker
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
from checkstyle.
from checkstyle.
Approved for test inputs
from checkstyle.
Related Issues (20)
- IllegalIdentifierName should be updated to support new Java 22 keywords HOT 2
- Add Check Support for Java 21 Pattern Matching for Switch Syntax: MagicNumber HOT 1
- Add Check Support for Java 17 Sealed Classes: Indentation HOT 4
- Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: Design Checks detected in #14942 HOT 3
- Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: Javadoc Checks HOT 4
- Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: RequireThis HOT 1
- Add Check Support for Java 21 Unnamed Variables & Patterns Syntax: Modifer Checks HOT 5
- MissingSwitchDefault : False positive when `case null` in switch labeled statement group HOT 2
- Outdated wiki description about imports HOT 8
- Checker framework being overly cautious while analyzing toArray( ) method. HOT 5
- Bdd violations messages should allow skip of leading and trailing '.*' HOT 11
- Update all Inputs that has inlined violations to avoid leading and trailing wildcard HOT 1
- RedundantModifer Should Violate Final Modifer on Unnamed Variables and Patterns HOT 3
- Update all JLS links for old versions HOT 1
- AnnotationOnSameLine: FalseNegative when the annotation is on the same line of the modifer of the target class HOT 1
- RegexMultiline contains Example7 that is actually part of Example6 HOT 7
- New Check: instanceof should be named or unnamed HOT 3
- NewCheck: UseEnhancedSwitch HOT 5
- IllegalTypeCheck: unclear handling of returns in examples HOT 1
- Conflict in Indentation Check Between `if` Parentheses and Record Pattern Parentheses
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from checkstyle.