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#15159: RedundantModifierCheck should violate final m…
…odifier on unnamed variables
- Loading branch information
Showing
8 changed files
with
287 additions
and
10 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
86 changes: 86 additions & 0 deletions
86
...kstyle/checks/modifier/redundantmodifier/InputRedundantModifierFinalUnnamedVariables.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,86 @@ | ||
/* | ||
RedundantModifier | ||
tokens = (default)METHOD_DEF, VARIABLE_DEF, ANNOTATION_FIELD_DEF, INTERFACE_DEF, \ | ||
CTOR_DEF, CLASS_DEF, ENUM_DEF, RESOURCE, PATTERN_VARIABLE_DEF, LITERAL_CATCH, LAMBDA | ||
jdkVersion = (default)22 | ||
*/ | ||
|
||
//non-compiled with javac: Compilable with Java21 | ||
package com.puppycrawl.tools.checkstyle.checks.modifier.redundantmodifier; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
public class InputRedundantModifierFinalUnnamedVariables { | ||
void m(Object o) { | ||
// violation below, 'Redundant 'final' modifier' | ||
if (o instanceof final String _) { } | ||
if (o instanceof final String __) { } | ||
if (o instanceof final String s) | ||
if (o instanceof final String _s) { } | ||
|
||
// violation below, 'Redundant 'final' modifier' | ||
final int _ = sideEffect(); | ||
final int __ = sideEffect(); | ||
final int x = sideEffect(); | ||
final int _x = sideEffect(); | ||
|
||
} | ||
|
||
void m2(Object o) { | ||
switch (o) { | ||
// violation below, 'Redundant 'final' modifier' | ||
case final String _ -> { } | ||
case Float _ -> { } | ||
case final Integer __ -> { } | ||
case final Double s -> { } | ||
default -> { } | ||
} | ||
} | ||
|
||
void m3() { | ||
// violation below, 'Redundant 'final' modifier' | ||
try (final var a = lock();) { | ||
|
||
} catch (final Exception e) { | ||
|
||
} | ||
|
||
// violation below, 'Redundant 'final' modifier' | ||
try (final var _ = lock();) { | ||
|
||
// violation below, 'Redundant 'final' modifier' | ||
} catch (final Exception _) { | ||
|
||
} | ||
} | ||
|
||
void m4() { | ||
BiFunction<Integer, Integer, Integer> f = (final Integer a, final Integer b) -> { | ||
return 5; | ||
}; | ||
|
||
// violation below, 'Redundant 'final' modifier' | ||
BiFunction<Integer, Integer, Integer> f2 = (final Integer _, final Integer b) -> { | ||
return 5; | ||
}; | ||
|
||
BiFunction<Integer, Integer, Integer> f3 = (final Integer _, final Integer _) -> { | ||
// 2 violations above: | ||
// 'Redundant 'final' modifier' | ||
// 'Redundant 'final' modifier' | ||
return 5; | ||
}; | ||
} | ||
|
||
int sideEffect() { | ||
return 0; | ||
} | ||
|
||
AutoCloseable lock() { | ||
return null; | ||
} | ||
|
||
record Point(int x, int y) { } | ||
} |
77 changes: 77 additions & 0 deletions
77
...modifier/redundantmodifier/InputRedundantModifierFinalUnnamedVariablesWithOldVersion.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,77 @@ | ||
/* | ||
RedundantModifier | ||
tokens = (default)METHOD_DEF, VARIABLE_DEF, ANNOTATION_FIELD_DEF, INTERFACE_DEF, \ | ||
CTOR_DEF, CLASS_DEF, ENUM_DEF, RESOURCE, PATTERN_VARIABLE_DEF, LITERAL_CATCH, LAMBDA | ||
jdkVersion = 8 | ||
*/ | ||
|
||
//non-compiled with javac: Compilable with Java21 | ||
package com.puppycrawl.tools.checkstyle.checks.modifier.redundantmodifier; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
public class InputRedundantModifierFinalUnnamedVariablesWithOldVersion { | ||
void m(Object o) { | ||
if (o instanceof final String _) { } | ||
if (o instanceof final String __) { } | ||
if (o instanceof final String s) | ||
if (o instanceof final String _s) { } | ||
|
||
final int _ = sideEffect(); | ||
final int __ = sideEffect(); | ||
final int x = sideEffect(); | ||
final int _x = sideEffect(); | ||
} | ||
|
||
void m2(Object o) { | ||
switch (o) { | ||
case final String _ -> { } | ||
case Float _ -> { } | ||
case final Integer __ -> { } | ||
case final Double s -> { } | ||
default -> { } | ||
} | ||
} | ||
|
||
void m3() { | ||
// violation below, 'Redundant 'final' modifier' | ||
try (final var a = lock();) { | ||
|
||
} catch (final Exception e) { | ||
|
||
} | ||
|
||
// violation below, 'Redundant 'final' modifier' | ||
try (final var _ = lock();) { | ||
|
||
} catch (final Exception _) { | ||
|
||
} | ||
} | ||
|
||
void m4() { | ||
BiFunction<Integer, Integer, Integer> f = (final Integer a, final Integer b) -> { | ||
return 5; | ||
}; | ||
|
||
BiFunction<Integer, Integer, Integer> f2 = (final Integer _, final Integer b) -> { | ||
return 5; | ||
}; | ||
|
||
BiFunction<Integer, Integer, Integer> f3 = (final Integer _, final Integer _) -> { | ||
return 5; | ||
}; | ||
} | ||
|
||
int sideEffect() { | ||
return 0; | ||
} | ||
|
||
AutoCloseable lock() { | ||
return null; | ||
} | ||
|
||
record Point(int x, int y) { } | ||
} |
Oops, something went wrong.