Skip to content

Commit f7e6d4e

Browse files
author
Vincent Potucek
committed
Pull #34816: Add checkstyle rule UnusedLocalVariable, UnusedLocalMethodCheck
Signed-off-by: Vincent Potucek <[email protected]>
1 parent 838b4d6 commit f7e6d4e

File tree

10 files changed

+30
-48
lines changed

10 files changed

+30
-48
lines changed

buildSrc/config/checkstyle/checkstyle.xml

+3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
<!-- Imports -->
1818
<module name="AvoidStarImport"/>
19+
<!-- Unused -->
1920
<module name="UnusedImports"/>
21+
<module name="UnusedLocalVariable"/>
22+
<!-- <module name="UnusedLocalMethodCheck"/> https://github.com/checkstyle/checkstyle/pull/16894 -->
2023
<module name="RedundantImport"/>
2124
<!-- Modifiers -->
2225
<module name="com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck"/>

buildSrc/src/test/java/org/springframework/build/multirelease/MultiReleaseJarPluginTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void packageInJar() throws IOException {
104104
writeClass("src/main/java17", "Main.java", """
105105
public class Main {}
106106
""");
107-
BuildResult buildResult = runGradle("assemble");
107+
runGradle("assemble");
108108
File file = new File(this.projectDir, "/build/libs/" + this.projectDir.getName() + "-1.2.3.jar");
109109
assertThat(file).exists();
110110
try (JarFile jar = new JarFile(file)) {

framework-docs/src/main/java/org/springframework/docs/core/aot/hints/testing/SampleReflectionRuntimeHintsTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ void shouldRegisterReflectionHints() {
4444

4545
// Invoke the relevant piece of code we want to test within a recording lambda
4646
RuntimeHintsInvocations invocations = org.springframework.aot.test.agent.RuntimeHintsRecorder.record(() -> {
47-
SampleReflection sample = new SampleReflection();
48-
sample.performReflection();
47+
new SampleReflection().performReflection();
4948
});
5049
// assert that the recorded invocations are covered by the contributed hints
5150
assertThat(invocations).match(runtimeHints);

integration-tests/src/test/java/org/springframework/aot/test/ReflectionInvocationsTests.java

+4-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.springframework.aot.hint.RuntimeHints;
2222
import org.springframework.aot.test.agent.EnabledIfRuntimeHintsAgent;
23-
import org.springframework.aot.test.agent.RuntimeHintsInvocations;
2423

2524
import static org.assertj.core.api.Assertions.assertThat;
2625

@@ -33,24 +32,17 @@ class ReflectionInvocationsTests {
3332
void sampleTest() {
3433
RuntimeHints hints = new RuntimeHints();
3534
hints.reflection().registerType(String.class);
36-
37-
RuntimeHintsInvocations invocations = org.springframework.aot.test.agent.RuntimeHintsRecorder.record(() -> {
38-
SampleReflection sample = new SampleReflection();
39-
sample.sample(); // does Method[] methods = String.class.getMethods();
40-
});
41-
assertThat(invocations).match(hints);
35+
// does Method[] methods = String.class.getMethods();
36+
assertThat(org.springframework.aot.test.agent.RuntimeHintsRecorder.record(new SampleReflection()::sample)).match(hints);
4237
}
4338

4439
@Test
4540
void multipleCallsTest() {
4641
RuntimeHints hints = new RuntimeHints();
4742
hints.reflection().registerType(String.class);
4843
hints.reflection().registerType(Integer.class);
49-
RuntimeHintsInvocations invocations = org.springframework.aot.test.agent.RuntimeHintsRecorder.record(() -> {
50-
SampleReflection sample = new SampleReflection();
51-
sample.multipleCalls(); // does Method[] methods = String.class.getMethods(); methods = Integer.class.getMethods();
52-
});
53-
assertThat(invocations).match(hints);
44+
// does String.class.getMethods(); Integer.class.getMethods();
45+
assertThat(org.springframework.aot.test.agent.RuntimeHintsRecorder.record(new SampleReflection()::multipleCalls)).match(hints);
5446
}
5547

5648
}

integration-tests/src/test/java/org/springframework/aot/test/SampleReflection.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.aot.test;
1818

19-
import java.lang.reflect.Method;
20-
2119
/**
2220
* @author Brian Clozel
2321
* @since 6.0
@@ -26,15 +24,13 @@ public class SampleReflection {
2624

2725
@SuppressWarnings("unused")
2826
public void sample() {
29-
String value = "Sample";
30-
Method[] methods = String.class.getMethods();
27+
String.class.getMethods();
3128
}
3229

3330
@SuppressWarnings("unused")
3431
public void multipleCalls() {
35-
String value = "Sample";
36-
Method[] methods = String.class.getMethods();
37-
methods = Integer.class.getMethods();
32+
String.class.getMethods();
33+
Integer.class.getMethods();
3834
}
3935

4036
}

spring-beans/src/test/java/org/springframework/beans/factory/config/ServiceLocatorFactoryBeanTests.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,9 @@ void testStringArgGetter() throws Exception {
122122
genericBeanDefinition(ServiceLocatorFactoryBean.class)
123123
.addPropertyValue("serviceLocatorInterface", TestServiceLocator2.class)
124124
.getBeanDefinition());
125-
126-
// test string-arg getter with null id
127-
TestServiceLocator2 factory = (TestServiceLocator2) bf.getBean("factory");
128-
129-
@SuppressWarnings("unused")
130-
TestService testBean = factory.getTestService(null);
131-
// now test with explicit id
132-
testBean = factory.getTestService("testService");
133-
// now verify failure on bad id
125+
TestServiceLocator2 factory = (TestServiceLocator2) bf.getBean("factory"); // test string-arg getter with null id
126+
factory.getTestService(null); // now test with explicit id
127+
factory.getTestService("testService"); // now verify failure on bad id
134128
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() ->
135129
factory.getTestService("bogusTestService"));
136130
}

spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -7145,8 +7145,7 @@ public TestClass9(int i) {
71457145
static class HttpServlet3RequestFactory {
71467146

71477147
static Servlet3SecurityContextHolderAwareRequestWrapper getOne() {
7148-
HttpServlet3RequestFactory outer = new HttpServlet3RequestFactory();
7149-
return outer.new Servlet3SecurityContextHolderAwareRequestWrapper();
7148+
return new HttpServlet3RequestFactory().new Servlet3SecurityContextHolderAwareRequestWrapper();
71507149
}
71517150

71527151
// private class Servlet3SecurityContextHolderAwareRequestWrapper extends SecurityContextHolderAwareRequestWrapper

spring-test/src/test/java/org/springframework/test/web/client/samples/SampleTests.java

+10-14
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ public void setup() {
6767
@Test
6868
public void performGet() {
6969

70-
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
70+
String responseBody = """
71+
{"name" : "Ludwig van Beethoven", "someDouble" : "1.6035"}""";
7172

7273
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
7374
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
7475

75-
@SuppressWarnings("unused")
7676
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
77+
assertThat(ludwig.getName()).isEqualTo("Ludwig van Beethoven");
78+
assertThat(ludwig.getSomeDouble()).isEqualTo(1.6035);
7779

7880
// We are only validating the request. The response is mocked out.
7981
// hotel.getId() == 42
@@ -90,8 +92,9 @@ public void performGetManyTimes() {
9092
this.mockServer.expect(manyTimes(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
9193
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
9294

93-
@SuppressWarnings("unused")
9495
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
96+
assertThat(ludwig.getName()).isEqualTo("Ludwig van Beethoven");
97+
assertThat(ludwig.getSomeDouble()).isEqualTo(1.6035);
9598

9699
// We are only validating the request. The response is mocked out.
97100
// hotel.getId() == 42
@@ -142,11 +145,9 @@ public void performGetWithResponseBodyFromFile() {
142145
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
143146
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
144147

145-
@SuppressWarnings("unused")
146148
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
147-
148-
// hotel.getId() == 42
149-
// hotel.getName().equals("Holiday Inn")
149+
assertThat(ludwig.getName()).isEqualTo("Ludwig van Beethoven");
150+
assertThat(ludwig.getSomeDouble()).isEqualTo(1.6035);
150151

151152
this.mockServer.verify();
152153
}
@@ -166,13 +167,8 @@ public void verify() {
166167
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
167168
.andRespond(withSuccess("8", MediaType.TEXT_PLAIN));
168169

169-
@SuppressWarnings("unused")
170-
String result1 = this.restTemplate.getForObject("/number", String.class);
171-
// result1 == "1"
172-
173-
@SuppressWarnings("unused")
174-
String result2 = this.restTemplate.getForObject("/number", String.class);
175-
// result == "2"
170+
assertThat(this.restTemplate.getForObject("/number", String.class)).isEqualTo("1");
171+
assertThat(this.restTemplate.getForObject("/number", String.class)).isEqualTo("2");
176172

177173
try {
178174
this.mockServer.verify();

spring-webmvc/src/test/java/org/springframework/web/servlet/handler/HandlerMethodMappingTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ void getCorsConfigWithBeanNameHandler() throws Exception {
291291

292292
this.mapping.setApplicationContext(context);
293293
this.mapping.registerMapping(key, beanName, this.method1);
294-
HandlerMethod handlerMethod = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
294+
this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
295295
}
296296

297297
@Test

src/checkstyle/checkstyle.xml

+3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@
9090

9191
<!-- Imports -->
9292
<module name="AvoidStarImport"/>
93+
<!-- Unused -->
9394
<module name="UnusedImports"/>
95+
<module name="UnusedLocalVariable"/>
96+
<!-- <module name="UnusedLocalMethodCheck"/> https://github.com/checkstyle/checkstyle/pull/16894 -->
9497
<module name="RedundantImport"/>
9598
<module name="com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck">
9699
<property name="groups" value="java,javax,*,org.springframework"/>

0 commit comments

Comments
 (0)