Skip to content

Commit c0b35a2

Browse files
apply CS:UnusedLocalVariable
Signed-off-by: Vincent Potucek <[email protected]>
1 parent c74f897 commit c0b35a2

File tree

10 files changed

+57
-76
lines changed

10 files changed

+57
-76
lines changed

.editorconfig

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
root = true
22

3-
[*.{adoc,bat,groovy,html,java,js,jsp,kt,kts,md,properties,py,rb,sh,sql,svg,txt,xml,xsd}]
3+
[*]
44
charset = utf-8
5+
end_of_line = lf
6+
# indent_style = tab todo: verify
7+
insert_final_newline = true
58

69
[*.{groovy,java,kt,kts,xml,xsd}]
710
indent_style = tab
811
indent_size = 4
9-
continuation_indent_size = 8
10-
end_of_line = lf
12+
ij_continuation_indent_size = 8
13+
14+
[*.java]
15+
ij_java_class_count_to_use_import_on_demand = 999
16+
ij_java_names_count_to_use_import_on_demand = 999

buildSrc/config/checkstyle/checkstyle.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<!-- Modifiers -->
2222
<module name="com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck"/>
2323

24+
<!-- Best Practice -->
25+
<module name="EmptyStatement"/>
26+
<module name="UnusedLocalVariable"/>
27+
<module name="VariableDeclarationUsageDistance"/>
2428
</module>
25-
2629
</module>

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

2525
import static org.assertj.core.api.Assertions.assertThat;
2626

@@ -34,23 +34,22 @@ void sampleTest() {
3434
RuntimeHints hints = new RuntimeHints();
3535
hints.reflection().registerType(String.class);
3636

37-
RuntimeHintsInvocations invocations = org.springframework.aot.test.agent.RuntimeHintsRecorder.record(() -> {
37+
assertThat(RuntimeHintsRecorder.record(() -> {
3838
SampleReflection sample = new SampleReflection();
39-
sample.sample(); // does Method[] methods = String.class.getMethods();
40-
});
41-
assertThat(invocations).match(hints);
39+
sample.sample(); // String.class.getMethods();
40+
})).match(hints);
4241
}
4342

4443
@Test
4544
void multipleCallsTest() {
4645
RuntimeHints hints = new RuntimeHints();
4746
hints.reflection().registerType(String.class);
4847
hints.reflection().registerType(Integer.class);
49-
RuntimeHintsInvocations invocations = org.springframework.aot.test.agent.RuntimeHintsRecorder.record(() -> {
48+
49+
assertThat(RuntimeHintsRecorder.record(() -> {
5050
SampleReflection sample = new SampleReflection();
51-
sample.multipleCalls(); // does Method[] methods = String.class.getMethods(); methods = Integer.class.getMethods();
52-
});
53-
assertThat(invocations).match(hints);
51+
sample.multipleCalls(); // String.class.getMethods(); Integer.class.getMethods();
52+
})).match(hints);
5453
}
5554

5655
}

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@
1616

1717
package org.springframework.aot.test;
1818

19-
import java.lang.reflect.Method;
20-
2119
/**
2220
* @author Brian Clozel
2321
* @since 6.0
2422
*/
2523
public class SampleReflection {
2624

27-
@SuppressWarnings("unused")
2825
public void sample() {
29-
String value = "Sample";
30-
Method[] methods = String.class.getMethods();
26+
String.class.getMethods();
3127
}
3228

33-
@SuppressWarnings("unused")
3429
public void multipleCalls() {
35-
String value = "Sample";
36-
Method[] methods = String.class.getMethods();
37-
methods = Integer.class.getMethods();
30+
String.class.getMethods();
31+
Integer.class.getMethods();
3832
}
3933

4034
}

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

+5-30
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,11 @@ void testStringArgGetter() throws Exception {
123123
.addPropertyValue("serviceLocatorInterface", TestServiceLocator2.class)
124124
.getBeanDefinition());
125125

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
134-
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() ->
135-
factory.getTestService("bogusTestService"));
126+
final var factory = (TestServiceLocator2) bf.getBean("factory"); // test string-arg getter
127+
factory.getTestService(null); // with null id
128+
factory.getTestService("testService"); // test with explicit id
129+
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
130+
.isThrownBy(() -> factory.getTestService("bogusTestService")); // verify failure on bad id
136131
}
137132

138133
@Disabled @Test // worked when using an ApplicationContext (see commented), fails when using BeanFactory
@@ -145,14 +140,6 @@ public void testCombinedLocatorInterface() {
145140
.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class)
146141
.getBeanDefinition());
147142

148-
// StaticApplicationContext ctx = new StaticApplicationContext();
149-
// ctx.registerPrototype("testService", TestService.class, new MutablePropertyValues());
150-
// ctx.registerAlias("testService", "1");
151-
// MutablePropertyValues mpv = new MutablePropertyValues();
152-
// mpv.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class);
153-
// ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
154-
// ctx.refresh();
155-
156143
TestServiceLocator3 factory = (TestServiceLocator3) bf.getBean("factory");
157144
TestService testBean1 = factory.getTestService();
158145
TestService testBean2 = factory.getTestService("testService");
@@ -177,16 +164,6 @@ public void testServiceMappings() {
177164
.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class)
178165
.addPropertyValue("serviceMappings", "=testService1\n1=testService1\n2=testService2")
179166
.getBeanDefinition());
180-
181-
// StaticApplicationContext ctx = new StaticApplicationContext();
182-
// ctx.registerPrototype("testService1", TestService.class, new MutablePropertyValues());
183-
// ctx.registerPrototype("testService2", ExtendedTestService.class, new MutablePropertyValues());
184-
// MutablePropertyValues mpv = new MutablePropertyValues();
185-
// mpv.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class);
186-
// mpv.addPropertyValue("serviceMappings", "=testService1\n1=testService1\n2=testService2");
187-
// ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
188-
// ctx.refresh();
189-
190167
TestServiceLocator3 factory = (TestServiceLocator3) bf.getBean("factory");
191168
TestService testBean1 = factory.getTestService();
192169
TestService testBean2 = factory.getTestService("testService1");
@@ -303,8 +280,6 @@ public interface TestService2Locator {
303280

304281
public interface ServiceLocatorInterfaceWithExtraNonCompliantMethod {
305282

306-
TestService2 getTestService();
307-
308283
TestService2 getTestService(String serviceName, String defaultNotAllowedParameter);
309284
}
310285

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

+11-19
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ public void performGet() {
7070
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
7171

7272
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
73-
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
73+
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
7474

75-
@SuppressWarnings("unused")
76-
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
75+
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
7776

7877
// We are only validating the request. The response is mocked out.
7978
// hotel.getId() == 42
@@ -90,8 +89,7 @@ public void performGetManyTimes() {
9089
this.mockServer.expect(manyTimes(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
9190
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
9291

93-
@SuppressWarnings("unused")
94-
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
92+
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
9593

9694
// We are only validating the request. The response is mocked out.
9795
// hotel.getId() == 42
@@ -142,8 +140,7 @@ public void performGetWithResponseBodyFromFile() {
142140
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
143141
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
144142

145-
@SuppressWarnings("unused")
146-
Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
143+
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
147144

148145
// hotel.getId() == 42
149146
// hotel.getName().equals("Holiday Inn")
@@ -155,24 +152,19 @@ public void performGetWithResponseBodyFromFile() {
155152
public void verify() {
156153

157154
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
158-
.andRespond(withSuccess("1", MediaType.TEXT_PLAIN));
155+
.andRespond(withSuccess("1", MediaType.TEXT_PLAIN));
159156

160157
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
161-
.andRespond(withSuccess("2", MediaType.TEXT_PLAIN));
158+
.andRespond(withSuccess("2", MediaType.TEXT_PLAIN));
162159

163160
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
164-
.andRespond(withSuccess("4", MediaType.TEXT_PLAIN));
161+
.andRespond(withSuccess("4", MediaType.TEXT_PLAIN));
165162

166163
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
167-
.andRespond(withSuccess("8", MediaType.TEXT_PLAIN));
168-
169-
@SuppressWarnings("unused")
170-
String result1 = this.restTemplate.getForObject("/number", String.class);
171-
// result1 == "1"
164+
.andRespond(withSuccess("8", MediaType.TEXT_PLAIN));
172165

173-
@SuppressWarnings("unused")
174-
String result2 = this.restTemplate.getForObject("/number", String.class);
175-
// result == "2"
166+
assertThat(this.restTemplate.getForObject("/number", String.class)).isEqualTo("1");
167+
assertThat(this.restTemplate.getForObject("/number", String.class)).isEqualTo("2");
176168

177169
try {
178170
this.mockServer.verify();
@@ -215,7 +207,7 @@ private ContentInterceptor(Resource resource) {
215207

216208
@Override
217209
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
218-
ClientHttpRequestExecution execution) throws IOException {
210+
ClientHttpRequestExecution execution) throws IOException {
219211

220212
ClientHttpResponse response = execution.execute(request, body);
221213
byte[] expected = FileCopyUtils.copyToByteArray(this.resource.getInputStream());

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

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

290290
this.mapping.setApplicationContext(context);
291291
this.mapping.registerMapping(key, beanName, this.method1);
292-
HandlerMethod handlerMethod = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
292+
this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
293293
}
294294

295295
@Test

src/checkstyle/checkstyle.xml

+4
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@
265265
<module name="io.spring.javaformat.checkstyle.check.SpringJavadocCheck"/>
266266
<module name="io.spring.javaformat.checkstyle.check.SpringJUnit5Check"/>
267267

268+
<!-- Best Practice -->
269+
<module name="EmptyStatement"/>
270+
<module name="UnusedLocalVariable"/>
271+
<!-- <module name="VariableDeclarationUsageDistance"/>--> <!-- wait for https://github.com/openrewrite/rewrite-static-analysis/issues/469 -->
268272
</module>
269273

270274
</module>

src/nohttp/checkstyle.xml

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<module name="Checker">
44
<property name="charset" value="UTF-8"/>
55
<property name="fileExtensions" value=""/>
6-
76
<!-- nohttp workaround, see https://github.com/spring-io/nohttp/issues/55 -->
87
<module name="io.spring.nohttp.checkstyle.check.NoHttpCheck">
98
<property name="allowlistFileName" value="${config_loc}/allowlist.lines" default=""/>
@@ -13,4 +12,14 @@
1312
<property name="optional" value="true"/>
1413
</module>
1514
<module name="SuppressWithPlainTextCommentFilter"/>
16-
</module>
15+
<!-- TreeWalker Checks -->
16+
<module name="TreeWalker">
17+
<!-- Imports -->
18+
<module name="AvoidStarImport"/>
19+
<module name="RedundantImport"/>
20+
<!-- ignored as samples need some flexibility https://github.com/spring-projects/spring-framework/pull/34477 -->
21+
<!-- <module name="UnusedImports"/>-->
22+
<!-- <module name="UnusedLocalVariable"/>-->
23+
<!-- <module name="VariableDeclarationUsageDistance"/>-->
24+
</module>
25+
</module>

0 commit comments

Comments
 (0)