diff --git a/java-checks-test-sources/default/src/main/java/checks/DeadStoreCheckSample.java b/java-checks-test-sources/default/src/main/java/checks/DeadStoreCheckSample.java index 14673587c45..c0c0792a50a 100644 --- a/java-checks-test-sources/default/src/main/java/checks/DeadStoreCheckSample.java +++ b/java-checks-test-sources/default/src/main/java/checks/DeadStoreCheckSample.java @@ -6,6 +6,7 @@ import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; +import java.util.List; import java.util.function.IntConsumer; import java.util.function.IntFunction; import java.util.function.IntUnaryOperator; @@ -13,6 +14,117 @@ class DeadStoreCheckSample { + record AnalysisResult(List issues) { + public AnalysisResult { + issues = List.copyOf(issues); + } + + AnalysisResult(List issues, boolean ignored) { + this(issues); + issues = List.of(); // Noncompliant {{Remove this useless assignment to local variable "issues".}} +// ^^^^^^^^^^^ + ignored = false; // Noncompliant {{Remove this useless assignment to local variable "ignored".}} +// ^^^^^^^ + } + + void update(List issues) { + issues = List.copyOf(issues); // Noncompliant {{Remove this useless assignment to local variable "issues".}} +// ^^^^^^^^^^^^^^^^^^^^^ + } + } + + record OverwrittenRecordParameter(List issues) { + OverwrittenRecordParameter { + issues = List.copyOf(issues); // Noncompliant {{Remove this useless assignment to local variable "issues".}} +// ^^^^^^^^^^^^^^^^^^^^^ + issues = List.of(); + } + } + + record AssignedRecordParameter(int value) { + AssignedRecordParameter { + value = 42; + int local = 42; // Noncompliant {{Remove this useless assignment to local variable "local".}} +// ^^^^ + } + } + + record ConditionalRecordParameters(int value, boolean replace) { + ConditionalRecordParameters { + value = 42; + if (replace) { + value = 24; + } + replace = false; + } + } + + record OverwrittenConditionalRecordParameter(int value, boolean replace) { + OverwrittenConditionalRecordParameter { + value = 42; // Noncompliant {{Remove this useless assignment to local variable "value".}} +// ^^^^ + if (replace) { + value = 24; + } else { + value = 12; + } + } + } + + record LoopRecordParameter(int value, boolean repeat) { + LoopRecordParameter { + value = 42; + while (repeat) { + value = 24; + repeat = false; + } + } + } + + record IncrementedRecordParameters(int value, int other) { + IncrementedRecordParameters { + value++; + ++other; + } + } + + record CompoundRecordParameter(int value) { + CompoundRecordParameter { + value += 2; // Noncompliant {{Remove this useless assignment to local variable "value".}} +// ^^^^ + value = 42; + value += 2; + } + } + + record NestedRecordParameter(int value) { + NestedRecordParameter { + value = 42; + class Local { + void update(int value) { + value = 24; // Noncompliant {{Remove this useless assignment to local variable "value".}} +// ^^^^ + } + } + } + } + + record ThrowingRecordParameter(int value) { + ThrowingRecordParameter { + value = 42; // Noncompliant {{Remove this useless assignment to local variable "value".}} +// ^^^^ + throw new IllegalArgumentException(); + } + } + + record ExplicitCanonicalConstructor(int value) { + ExplicitCanonicalConstructor(int value) { + this.value = value; + value = 42; // Noncompliant {{Remove this useless assignment to local variable "value".}} +// ^^^^ + } + } + int var; class ConstructorDeadStores { diff --git a/java-frontend/src/main/java/org/sonar/java/cfg/CFG.java b/java-frontend/src/main/java/org/sonar/java/cfg/CFG.java index 0ad03387eeb..e94f778852f 100644 --- a/java-frontend/src/main/java/org/sonar/java/cfg/CFG.java +++ b/java-frontend/src/main/java/org/sonar/java/cfg/CFG.java @@ -498,7 +498,11 @@ public static CFG buildCFG(List trees) { public static CFG build(MethodTree tree) { BlockTree block = tree.block(); Preconditions.checkArgument(block != null, "Cannot build CFG for method with no body."); - return new CFG(block.body(), tree); + return build(tree, block.body()); + } + + public static CFG build(MethodTree tree, List elements) { + return new CFG(elements, tree); } private void build(ListTree trees) { diff --git a/java-frontend/src/main/java/org/sonar/java/model/declaration/MethodTreeImpl.java b/java-frontend/src/main/java/org/sonar/java/model/declaration/MethodTreeImpl.java index ee989957a6e..5cde28f969b 100644 --- a/java-frontend/src/main/java/org/sonar/java/model/declaration/MethodTreeImpl.java +++ b/java-frontend/src/main/java/org/sonar/java/model/declaration/MethodTreeImpl.java @@ -18,8 +18,10 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Set; import javax.annotation.Nullable; import org.sonar.java.Preconditions; import org.sonar.java.ast.parser.FormalParametersListTreeImpl; @@ -32,6 +34,7 @@ import org.sonar.java.model.ModifiersUtils; import org.sonar.plugins.java.api.semantic.Symbol; import org.sonar.plugins.java.api.tree.AnnotationTree; +import org.sonar.plugins.java.api.tree.BaseTreeVisitor; import org.sonar.plugins.java.api.tree.BlockTree; import org.sonar.plugins.java.api.tree.ExpressionTree; import org.sonar.plugins.java.api.tree.IdentifierTree; @@ -249,11 +252,31 @@ public CFG cfg() { return null; } if (cfg == null) { - cfg = CFG.build(this); + // Compact record constructors have no parameter-list parentheses. + cfg = is(Tree.Kind.CONSTRUCTOR) && openParenToken == null + ? CFG.build(this, compactConstructorElements(block)) + : CFG.build(this); } return cfg; } + private List compactConstructorElements(BlockTree body) { + List elements = new ArrayList<>(body.body()); + Set params = new HashSet<>(); + Symbol.MethodSymbol constructorSymbol = symbol(); + body.accept(new BaseTreeVisitor() { + @Override + public void visitIdentifier(IdentifierTree identifier) { + Symbol identSymbol = identifier.symbol(); + if (identSymbol.isParameter() && constructorSymbol.equals(identSymbol.owner()) && params.add(identSymbol)) { + // Add an artificial read to model the implicit assignment to the record field. + elements.add(identifier); + } + } + }); + return elements; + } + @Override public List children() { List list = new ArrayList<>(); diff --git a/java-frontend/src/test/java/org/sonar/java/model/declaration/MethodTreeImplTest.java b/java-frontend/src/test/java/org/sonar/java/model/declaration/MethodTreeImplTest.java index 6ee04398295..20a68b0db7c 100644 --- a/java-frontend/src/test/java/org/sonar/java/model/declaration/MethodTreeImplTest.java +++ b/java-frontend/src/test/java/org/sonar/java/model/declaration/MethodTreeImplTest.java @@ -18,12 +18,17 @@ import java.util.List; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.sonar.java.cfg.CFG; import org.sonar.java.model.JParserTestUtils; import org.sonar.plugins.java.api.cfg.ControlFlowGraph; import org.sonar.plugins.java.api.semantic.Symbol; import org.sonar.plugins.java.api.tree.ClassTree; import org.sonar.plugins.java.api.tree.CompilationUnitTree; +import org.sonar.plugins.java.api.tree.IdentifierTree; import org.sonar.plugins.java.api.tree.MethodTree; +import org.sonar.plugins.java.api.tree.StatementTree; import org.sonar.plugins.java.api.tree.Tree; import static org.assertj.core.api.Assertions.assertThat; @@ -220,6 +225,64 @@ void compute_cfg() { assertThat(methodWithSwitchAndYield.cfg()).isSameAs(cfg); } + @Test + void compact_constructor_cfg_reads_final_parameter_value_without_modifying_body() { + MethodTreeImpl method = getUniqueMethod("record R(int value) { R { value = 42; value = 24; } }"); + List statements = List.copyOf(method.block().body()); + + CFG cfg = method.cfg(); + + assertThat(cfg.entryBlock().elements()).extracting(Tree::kind) + .containsExactly(Tree.Kind.INT_LITERAL, Tree.Kind.ASSIGNMENT, Tree.Kind.INT_LITERAL, Tree.Kind.ASSIGNMENT, Tree.Kind.IDENTIFIER); + IdentifierTree implicitRead = (IdentifierTree) cfg.entryBlock().elements().getLast(); + assertThat(implicitRead.symbol().name()).isEqualTo("value"); + assertThat(implicitRead.symbol().owner()).isEqualTo(method.symbol()); + assertThat(cfg.methodSymbol()).isSameAs(method.symbol()); + assertThat(method.cfg()).isSameAs(cfg); + assertThat(method.block().body()).containsExactlyElementsOf(statements); + } + + @ParameterizedTest + @ValueSource(strings = { + "class R { R(int value) { value = 42; } }", + "record R(int value) { R(int value) { this.value = value; value = 42; } }", + "record R(int value) { R(int value, boolean ignored) { this(value); ignored = false; } }" + }) + void explicit_constructor_cfg_has_no_implicit_parameter_reads(String source) { + MethodTreeImpl method = getUniqueMethod(source); + + CFG cfg = method.cfg(); + + assertThat(cfg.entryBlock().elements()).extracting(Tree::kind).endsWith(Tree.Kind.ASSIGNMENT); + } + + @Test + void compact_constructor_cfg_only_adds_reads_for_its_own_parameters() { + MethodTreeImpl method = getUniqueMethod(""" + record R(int value) { + R { + int local = 42; + value = local; + class Local { + void update(int value) { + value = 24; + } + } + } + } + """); + + CFG cfg = method.cfg(); + + assertThat(cfg.entryBlock().elements()) + .filteredOn(tree -> tree.is(Tree.Kind.IDENTIFIER)) + .map(tree -> ((IdentifierTree) tree).symbol()) + .extracting(Symbol::name) + .containsExactly("local", "value"); + IdentifierTree implicitRead = (IdentifierTree) cfg.entryBlock().elements().getLast(); + assertThat(implicitRead.symbol().owner()).isEqualTo(method.symbol()); + } + @Test void has_all_syntax_token() { MethodTreeImpl method = getUniqueMethod("class A { public void foo(int arg) throws Exception {} }");