Skip to content

Commit aed4840

Browse files
SONARJAVA-5274 Fix FP in S1123 on deprecated record fields. (#5122)
1 parent 62429e1 commit aed4840

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package checks;
22

3-
record MissingDeprecatedCheckWithRecords(@Deprecated int lo, int hi) { // Noncompliant
3+
record MissingDeprecatedCheckWithRecords(@Deprecated int lo, int hi) {
44
public MissingDeprecatedCheckWithRecords {
55
@Deprecated
66
int x = 42;
@@ -9,3 +9,12 @@ record MissingDeprecatedCheckWithRecords(@Deprecated int lo, int hi) { // Noncom
99
@Deprecated
1010
void foo() {} // Noncompliant
1111
}
12+
13+
// This is a dangling JavaDoc, which is not supported, so we do not require annotations.
14+
record Person(
15+
String name,
16+
/**
17+
* @deprecated
18+
*/
19+
String address // Compliant
20+
) {}

java-checks-test-sources/default/src/main/java/checks/MissingDeprecatedCheckSample.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,34 @@ public int foo11() { // Compliant
8383
return 42;
8484
}
8585

86+
record Person(String name, @Deprecated(since = "date", forRemoval = true) String country) {
87+
@Deprecated
88+
public static final int code = 41; // Noncompliant
89+
90+
/**
91+
* @deprecated reason
92+
*/
93+
@Deprecated
94+
public static final int anotherCode = 42;
95+
96+
public Person {
97+
@Deprecated
98+
int x = 42;
99+
}
100+
101+
@Deprecated
102+
public int number() { // Noncompliant
103+
return 0;
104+
}
105+
106+
/**
107+
* @deprecated reason
108+
*/
109+
@Deprecated
110+
public int anotherNumber() {
111+
return 1;
112+
}
113+
}
86114
}
87115

88116
interface MissingDeprecatedCheckSample_Bar {

java-checks/src/main/java/org/sonar/java/checks/MissingDeprecatedCheck.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
package org.sonar.java.checks;
1818

19+
import java.util.Optional;
1920
import javax.annotation.CheckForNull;
2021
import org.sonar.check.Rule;
2122
import org.sonar.plugins.java.api.tree.AnnotationTree;
2223
import org.sonar.plugins.java.api.tree.Tree;
24+
import org.sonar.plugins.java.api.tree.VariableTree;
2325
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
2426

2527
import static org.sonar.java.checks.helpers.DeprecatedCheckerHelper.reportTreeForDeprecatedTree;
@@ -29,6 +31,11 @@
2931
public class MissingDeprecatedCheck extends AbstractMissingDeprecatedChecker {
3032

3133
void handleDeprecatedElement(Tree tree, @CheckForNull AnnotationTree deprecatedAnnotation, boolean hasJavadocDeprecatedTag) {
34+
// Record fields cannot have JavaDocs, so skip the check.
35+
if (isRecordComponent(tree)) {
36+
return;
37+
}
38+
3239
boolean hasDeprecatedAnnotation = deprecatedAnnotation != null;
3340
if (hasDeprecatedAnnotation) {
3441
if (!hasJavadocDeprecatedTag) {
@@ -39,4 +46,15 @@ void handleDeprecatedElement(Tree tree, @CheckForNull AnnotationTree deprecatedA
3946
}
4047
}
4148

49+
/**
50+
* Checks whether the argument is a component of a record (a non-static field).
51+
*/
52+
private static boolean isRecordComponent(Tree tree) {
53+
if (tree instanceof VariableTree variableTree && !variableTree.symbol().isStatic()) {
54+
return Optional.ofNullable(tree.parent())
55+
.filter(parent -> parent.is(Tree.Kind.RECORD))
56+
.isPresent();
57+
}
58+
return false;
59+
}
4260
}

0 commit comments

Comments
 (0)