Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,125 @@
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;
import java.util.function.Supplier;

class DeadStoreCheckSample {

record AnalysisResult(List<Integer> issues) {
public AnalysisResult {
issues = List.copyOf(issues);
}

AnalysisResult(List<Integer> 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<Integer> issues) {
issues = List.copyOf(issues); // Noncompliant {{Remove this useless assignment to local variable "issues".}}
// ^^^^^^^^^^^^^^^^^^^^^
}
}

record OverwrittenRecordParameter(List<Integer> 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 {
Expand Down
6 changes: 5 additions & 1 deletion java-frontend/src/main/java/org/sonar/java/cfg/CFG.java
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,11 @@ public static CFG buildCFG(List<? extends Tree> 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<? extends Tree> elements) {
return new CFG(elements, tree);
}

private void build(ListTree<? extends Tree> trees) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Tree> compactConstructorElements(BlockTree body) {
List<Tree> elements = new ArrayList<>(body.body());
Set<Symbol> 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<Tree> children() {
List<Tree> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<StatementTree> 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 {} }");
Expand Down
Loading