Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"eclipse-jetty:jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/WebSocketUriMappingTest.java": [
91
],
"eclipse-jetty:jetty-server/src/main/java/org/eclipse/jetty/server/handler/jmx/AbstractHandlerMBean.java": [
82
],
"eclipse-jetty:jetty-server/src/test/java/org/eclipse/jetty/server/HttpOutputTest.java": [
861
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"eclipse-jetty:jetty-http/src/test/java/org/eclipse/jetty/http/pathmap/WebSocketUriMappingTest.java": [
91
],
"eclipse-jetty:jetty-server/src/main/java/org/eclipse/jetty/server/handler/jmx/AbstractHandlerMBean.java": [
82
],
"eclipse-jetty:jetty-server/src/test/java/org/eclipse/jetty/server/HttpOutputTest.java": [
861
],
Expand Down Expand Up @@ -80,6 +83,11 @@
175,
188
],
"eclipse-jetty:jetty-util-ajax/src/main/java/org/eclipse/jetty/util/ajax/AsyncJSON.java": [
586,
629,
675
],
"eclipse-jetty:jetty-util/src/main/java/org/eclipse/jetty/util/IO.java": [
149,
191
Expand Down
3 changes: 3 additions & 0 deletions its/ruling/src/test/resources/expected/guava/java-S1854.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"guava:src/com/google/common/base/CaseFormat.java": [
149
],
"guava:src/com/google/common/cache/LocalCache.java": [
2331,
2844,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ void lambdas_not_resolved(UnknnownFunction lambda) {
int x = 1; // Compliant
return y + x;
});
lambdas_not_resolved(y -> ++y);
lambdas_not_resolved(y -> y += 2);
}

void fpLambdaWithAssignment(boolean cond) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
package checks;

import com.google.common.collect.Lists;

import java.lang.Exception;
import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.function.IntUnaryOperator;
import java.util.function.Supplier;

class DeadStoreCheckSample {

int var;

class ConstructorDeadStores {
ConstructorDeadStores(int value) {
int x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = 3;
value = compute(); // Noncompliant {{Remove this useless assignment to local variable "value".}}
// ^^^^^^^^^^^
value = x;
var = value;
}
}

class ConstructorWithLambdaCapture {
ConstructorWithLambdaCapture(boolean condition) {
int captured = compute();
if (condition) {
System.out.println("branch");
}
Runnable runnable = () -> {
int unused = compute(); // Noncompliant {{Remove this useless assignment to local variable "unused".}}
// ^^^^^^^^^^^
System.out.println(captured);
};
runnable.run();
}
}

void singleAssignment(){
int z = 10; // Noncompliant
}
Expand All @@ -33,6 +65,14 @@ int foo(int u) {
return 0;
}

void dead_store_in_condition(BufferedReader reader) throws IOException {
String line;
if ((line = reader.readLine()) != null) { // Noncompliant {{Remove this useless assignment to local variable "line".}}
// ^^^^^^^^^^^^^^^^^^^
System.out.println("Line available");
}
}

void fields() {
this.var = 2; // Compliant
var = 3; // Compliant - do not check fields
Expand All @@ -51,6 +91,213 @@ void lambdas_using_local_vars(IntFunction lambda) {
lambdas_using_local_vars(y -> a + y);
}

void dead_store_in_lambda() {
int captured = compute();
Runnable r = () -> {
int live = compute();
int x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = 3;
System.out.println(x + live + captured);
};
r.run();
}

IntFunction<Integer> dead_store_in_lambda_parameter() {
return x -> {
x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = 3;
return x;
};
}

IntFunction<Integer> assignment_result_used_in_block_lambda() {
return x -> {
int y = x = x + 1; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^
return y;
};
}

IntFunction<Integer> dead_store_in_expression_lambda() {
return x -> x++; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^
}

IntConsumer prefix_increment_lambda = x -> ++x; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^

IntConsumer compound_assignment_lambda = x -> x += 2; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^

IntConsumer assignment_lambda = x -> x = 2; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^

IntConsumer prefix_increment_block_lambda = x -> {
System.out.println(++x);
++x; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^
};

IntConsumer compound_assignment_block_lambda = x -> {
System.out.println(x += 2);
x += 2; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^
};

IntUnaryOperator prefix_increment_result_lambda = x -> ++x; // Noncompliant {{Remove this useless assignment to local variable "x".}}
IntUnaryOperator compound_assignment_result_lambda = x -> x += 2; // Noncompliant {{Remove this useless assignment to local variable "x".}}
IntUnaryOperator assignment_result_lambda = x -> x = x + 1; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^
IntUnaryOperator assignment_result_block_lambda = x -> {
return x = x + 1; // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^
};

Runnable prefix_increment_field_lambda = () -> ++var;
Runnable compound_assignment_field_lambda = () -> var += 2;
Runnable empty_lambda = () -> {};

Runnable dead_store_in_nested_lambda(boolean condition) {
return () -> {
int captured = compute();
if (condition) {
System.out.println("branch");
}
Runnable nested = () -> {
int x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = captured;
System.out.println(x);
};
nested.run();
};
}

Supplier<Runnable> expression_lambda_returning_lambda() {
return () -> () -> {
int x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = 3;
System.out.println(x);
};
}

Runnable classes_in_lambda_capturing_local_variables(boolean condition) {
return () -> {
int capturedByAnonymousClass = compute();
int capturedByLocalClass = compute();
class Local {
void print() {
System.out.println(capturedByLocalClass);
}
}
if (condition) {
System.out.println("branch");
}
new Runnable() {
@Override
public void run() {
System.out.println(capturedByAnonymousClass);
new Local().print();
}
}.run();
};
}

Runnable method_reference_in_lambda_using_local_variable(boolean condition) {
return () -> {
Object captured = new Object();
if (condition) {
System.out.println("branch");
}
Supplier<String> reference = captured::toString;
System.out.println(reference.get());
};
}

IntFunction<Integer> lambda_with_conditional_assignment() {
return condition -> {
int x = compute();
if (condition > 0) {
x = compute(); // Noncompliant {{Remove this useless assignment to local variable "x".}}
// ^^^^^^^^^^^
x = 3;
}
return x;
};
}

Runnable lambda_using_local_variable_in_finally() {
return () -> {
int x = compute();
try {
x = compute();
} finally {
System.out.println(x);
}
};
}

Runnable lambda_using_local_variable_in_catch() {
return () -> {
int x = compute();
try {
x = compute();
System.out.println(x);
} catch (RuntimeException e) {
System.out.println(x);
}
};
}

Runnable lambda_with_resource_used_only_by_implicit_close() {
return () -> {
try (FileInputStream stream = new FileInputStream("...")) {
} catch (java.io.IOException e) {
throw new java.io.UncheckedIOException(e);
}
};
}

Runnable dead_store_in_lambda_inside_try_finally() {
int x = compute();
try {
return () -> {
int y = compute(); // Noncompliant {{Remove this useless assignment to local variable "y".}}
// ^^^^^^^^^^^
y = 3;
System.out.println(y);
};
} finally {
System.out.println(x);
}
}

Runnable try_finally_in_nested_lambda_should_not_exclude_enclosing_scopes() {
int unused = compute(); // Noncompliant {{Remove this useless assignment to local variable "unused".}}
// ^^^^^^^^^^^
int capturedByOuterLambda = compute();
return () -> {
int unusedInLambda = compute(); // Noncompliant {{Remove this useless assignment to local variable "unusedInLambda".}}
// ^^^^^^^^^^^
int capturedByInnerLambda = compute();
Runnable nested = () -> {
try {
System.out.println("try");
} finally {
System.out.println(capturedByOuterLambda + capturedByInnerLambda);
}
};
nested.run();
};
}

private int compute() {
return 1;
}

void ignore_try_finally() {
int a;
a = 12; //false negative excluded by try finally
Expand Down Expand Up @@ -137,15 +384,15 @@ int increment_operator() {
++b; // Noncompliant
// ^^^
int c = 0;
foo(++c); // compliant not last element of block
foo(++c); // Noncompliant {{Remove this useless assignment to local variable "c".}}
int j = -1;
while ((j = foo(++j)) != -1) {
while ((j = foo(++j)) != -1) { // Noncompliant {{Remove this useless assignment to local variable "j".}}
System.out.println("");
}
if(i != 0) {
return i++; // Noncompliant
} else {
return ++i;
return ++i; // Noncompliant
}
}

Expand Down Expand Up @@ -226,7 +473,7 @@ int foo() {
n = 2;
n += 12; // Noncompliant
int order = 0;
return (short) (order &= 12);
return (short) (order &= 12); // Noncompliant
}
}

Expand Down
Loading
Loading