SONARJAVA-6827: Implement S9357 Anonymous classes on functional interfaces should be lambdas - #5995
SONARJAVA-6827: Implement S9357 Anonymous classes on functional interfaces should be lambdas#5995romainbrenguier wants to merge 1 commit into
Conversation
…faces should be lambdas Detects anonymous inner classes implementing functional interfaces (single abstract method) that can be replaced with lambda expressions. Mirrors the detection logic of S1604 but applies to all code (main and test scope). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| public class AnonymousClassOnFunctionalInterfaceCheck extends BaseTreeVisitor implements JavaFileScanner, JavaVersionAwareVisitor { | ||
|
|
||
| private static final String JAVA_LANG_OBJECT = "java.lang.Object"; | ||
| private JavaFileScannerContext context; | ||
| private final Set<IdentifierTree> enumConstants = new HashSet<>(); | ||
|
|
||
| @Override | ||
| public boolean isCompatibleWithJavaVersion(JavaVersion version) { | ||
| return version.isJava8Compatible(); | ||
| } | ||
|
|
||
| @Override | ||
| public void scanFile(JavaFileScannerContext context) { | ||
| this.context = context; | ||
| enumConstants.clear(); |
There was a problem hiding this comment.
⚠️ Quality: New check duplicates S1604 logic verbatim
AnonymousClassOnFunctionalInterfaceCheck is a byte-for-byte copy of AnonymousClassShouldBeLambdaCheck (S1604), differing only in the @rule key and class name. This duplicates ~150 lines including the UsesThisInstanceVisitor, isSAM/hasSingleAbstractMethodInHierarchy logic, so any future bug fix or improvement must be applied twice and will drift. Extract the shared detection logic into a common base/utility class that both S1604 and S9357 delegate to, so the two rules only differ in their @rule key and scope metadata.
Was this helpful? React with 👍 / 👎
| "defaultSeverity": "Major", | ||
| "ruleSpecification": "RSPEC-9357", | ||
| "sqKey": "S9357", | ||
| "scope": "All", |
There was a problem hiding this comment.
⚠️ Bug: S9357 double-reports with S1604 on main code
S9357 has scope "All" (main + test) while S1604 has scope "Main", and both implement identical detection with an identical message. When both rules are active in a quality profile, every anonymous-class-to-lambda opportunity in main code will be flagged twice (once as S1604, once as S9357), producing duplicate issues for users. Confirm this is intended; if S9357 is only meant to extend S1604's coverage to test code, its scope/activation should avoid overlapping with S1604 on main code (or S1604 should be deprecated in favor of S9357).
Was this helpful? React with 👍 / 👎
CI failed: Integration test failures in JavaRulingTest due to issue differences (42 differences on Windows and diff reports on Linux) caused by the newly introduced rule S9357.OverviewAll 3 analyzed CI logs across Linux and Windows runners failed during the ruling integration tests ( FailuresRuling Integration Test Mismatch (confidence: high)
Summary
Code Review
|
| Auto-apply | Compact | Unblock |
|
|
|
Was this helpful? React with 👍 / 👎 | Gitar
|
|
|
❌ Ruling needs updating. A fix PR has been created: #5996 Please review and merge it into your branch. |


Summary
AnonymousClassShouldBeLambdaCheck) but with scope"All"(main + test code) instead of"Main"onlythisusage, checked exceptions, annotations, default methods, recursion, generic methods, enum constants)Test plan
🤖 Generated with Claude Code