Skip to content

Commit de8e95b

Browse files
SONARJAVA-6824: Fix hasExactlyOneArgumentDifference to count differences and add ruling expectations
The method was using a boolean flag that returned true when any arguments differed, causing false positives for cases with multiple differing arguments. Now uses an integer counter to ensure exactly one argument differs. Also adds eclipse-jetty ruling expectations and test cases for multiple argument differences. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent bc72478 commit de8e95b

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"org.eclipse.jetty:jetty-project:jetty-http/src/main/java/org/eclipse/jetty/http/QuotedQualityCSV.java": [
3+
129
4+
],
5+
"org.eclipse.jetty:jetty-project:jetty-jmx/src/main/java/org/eclipse/jetty/jmx/MBeanContainer.java": [
6+
362,
7+
373
8+
],
9+
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/JavaVersion.java": [
10+
58
11+
],
12+
"org.eclipse.jetty:jetty-project:jetty-util/src/main/java/org/eclipse/jetty/util/ssl/SniX509ExtendedKeyManager.java": [
13+
178,
14+
192
15+
],
16+
"org.eclipse.jetty:jetty-project:jetty-util/src/test/java/org/eclipse/jetty/util/statistic/CounterStatisticTest.java": [
17+
91
18+
]
19+
}

‎java-checks-test-sources/default/src/main/java/checks/TernaryOperatorSameOperationCheckSample.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ void testMethodInvocations() {
2626
// Method invocations with multiple args where some differ - Noncompliant
2727
String m5 = condition ? foo(a, x) : foo(b, x); // Noncompliant
2828

29+
// Method invocations with multiple args where all differ - Compliant
30+
String m6 = condition ? foo(a, x) : foo(b, y); // Compliant - more than one argument differs
31+
2932
// Method invocations - Compliant (different operations or same arguments)
3033
String c1 = condition ? foo(a) : bar(b); // Compliant - different methods
3134
String c2 = condition ? foo(a) : foo(a); // Compliant - same arguments
@@ -63,6 +66,9 @@ void testNewClass() {
6366
// New class with multiple args where some differ - Noncompliant
6467
Object n2 = condition ? new Foo(a, b) : new Foo(b, b); // Noncompliant
6568

69+
// New class with multiple args where all differ - Compliant
70+
Object n3 = condition ? new Foo(a, a) : new Foo(b, b); // Compliant - more than one argument differs
71+
6672
// New class - Compliant
6773
Object c4 = condition ? new Foo(a) : new Bar(b); // Compliant - different classes
6874
Object c5 = condition ? new Foo(a) : new Foo(a); // Compliant - same arguments

‎java-checks/src/main/java/org/sonar/java/checks/TernaryOperatorSameOperationCheck.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ private static boolean hasExactlyOneArgumentDifference(List<? extends Expression
9797
if (leftArgs.size() != rightArgs.size()) {
9898
return false;
9999
}
100-
boolean anyDifferent = false;
100+
int differences = 0;
101101
for (int i = 0; i < leftArgs.size(); i++) {
102102
if (!sameTree(leftArgs.get(i), rightArgs.get(i))) {
103-
anyDifferent = true;
103+
differences++;
104104
}
105105
}
106-
return anyDifferent;
106+
return differences == 1;
107107
}
108108

109109
private static boolean sameArrayAccess(ArrayAccessExpressionTree left, ArrayAccessExpressionTree right) {

0 commit comments

Comments
 (0)