forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#15445: Update NPathComplexity Check to Account for w…
…hen expression as a Possible Execution Path
- Loading branch information
Showing
7 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...l/tools/checkstyle/checks/metrics/npathcomplexity/InputNPathComplexityWhenExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
NPathComplexity | ||
max = 1 | ||
*/ | ||
|
||
//non-compiled with javac: Compilable with Java21 | ||
package com.puppycrawl.tools.checkstyle.checks.metrics.npathcomplexity; | ||
|
||
public class InputNPathComplexityWhenExpression { | ||
|
||
// violation below, 'NPath Complexity is 3 (max allowed is 1)' | ||
void m(Object o) { | ||
|
||
if (o instanceof String s && !s.isEmpty()) { } | ||
} | ||
|
||
// violation below, 'NPath Complexity is 3 (max allowed is 1)' | ||
void m2(Object o) { | ||
switch (o) { | ||
case String s when !s.isEmpty() -> { } | ||
default -> { } | ||
} | ||
} | ||
|
||
// violation below, 'NPath Complexity is 3 (max allowed is 1)' | ||
void m3(Object o) { | ||
switch (o) { | ||
case String s when (!s.isEmpty()) -> { } | ||
default -> { } | ||
} | ||
} | ||
|
||
// violation below, 'NPath Complexity is 4 (max allowed is 1)' | ||
void m4(Object o, boolean b) { | ||
switch (o) { | ||
case String s when !s.isEmpty() && b -> { } | ||
default -> { } | ||
} | ||
} | ||
|
||
// violation below, 'NPath Complexity is 4 (max allowed is 1)' | ||
void m5(Object o, boolean b) { | ||
switch (o) { | ||
case String s when (!s.isEmpty() && b) -> { } | ||
default -> { } | ||
} | ||
} | ||
|
||
// violation below, 'NPath Complexity is 5 (max allowed is 1)' | ||
void m6(Object o, boolean b, boolean c) { | ||
switch (o) { | ||
case String s when (!s.isEmpty() && b) && c -> { } | ||
default -> { } | ||
} | ||
} | ||
|
||
// violation below, 'NPath Complexity is 7 (max allowed is 1)' | ||
void m7(Object o, boolean b, boolean c) { | ||
switch (o) { | ||
case String s when !s.isEmpty() -> { } | ||
case Integer i when (i == 0) && c || b -> { } | ||
default -> { } | ||
} | ||
} | ||
|
||
// violation below, 'NPath Complexity is 5 (max allowed is 1)' | ||
void m8(Object o, boolean b, boolean c) { | ||
switch (o) { | ||
case String s when (b ? true : c) -> { } | ||
default -> { } | ||
} | ||
} | ||
|
||
// violation below, 'NPath Complexity is 5 (max allowed is 1)' | ||
void m9(Object o, boolean b, boolean c) { | ||
switch (o) { | ||
case String s when b ? true : c -> { } | ||
default -> { } | ||
} | ||
} | ||
|
||
// violation below, 'NPath Complexity is 6 (max allowed is 1)' | ||
void m10(Object o, boolean b, boolean c) { | ||
switch (o) { | ||
case String s when (b ? true : c) && c == true -> { } | ||
default -> { } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters