Skip to content

Commit 5c63107

Browse files
authored
Suppress/avoid a redundant variable warning when injecting a Java expression (#112)
1 parent a052145 commit 5c63107

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

src/main/java/org/mapstruct/intellij/expression/JavaExpressionInjector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ else if ( resolved instanceof PsiParameter ) {
216216
if ( appendTypeParametersHard( prefixBuilder, imports, method.getTypeParameters() ) ) {
217217
prefixBuilder.append( " " );
218218
}
219-
prefixBuilder.append( "void __test__(\n" );
219+
appendType( prefixBuilder, imports, targetType );
220+
prefixBuilder.append( " __test__(\n" );
220221

221222
PsiParameter[] parameters = method.getParameterList().getParameters();
222223
for ( int i = 0; i < parameters.length; i++ ) {
@@ -239,8 +240,7 @@ else if ( resolved instanceof PsiParameter ) {
239240
appendNesting( prefixBuilder, 1 );
240241
prefixBuilder.append( ") {\n" );
241242
appendNesting( prefixBuilder, 2 );
242-
appendType( prefixBuilder, imports, targetType );
243-
prefixBuilder.append( " __target__ = " );
243+
prefixBuilder.append( "return " );
244244

245245
PsiAnnotation mapper = mapperClass.getAnnotation( MapstructUtil.MAPPER_ANNOTATION_FQN );
246246
if ( mapper != null ) {

src/test/java/org/mapstruct/intellij/expression/JavaExpressionInjectionTest.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ protected void withMapperWithImports(String attribute) {
304304
"abstract class CarMapperImpl\n" +
305305
" implements CarMapper {\n" +
306306
"\n" +
307-
" void __test__(\n" +
307+
" String __test__(\n" +
308308
" Car car\n" +
309309
" ) {\n" +
310-
" String __target__ = Collections;\n" +
310+
" return Collections;\n" +
311311
" }\n" +
312312
"}" );
313313

@@ -332,10 +332,10 @@ protected void withMapperWithCustomImports(String attribute) {
332332
"abstract class CarMapperImpl\n" +
333333
" implements CarMapper {\n" +
334334
"\n" +
335-
" void __test__(\n" +
335+
" String __test__(\n" +
336336
" Car car\n" +
337337
" ) {\n" +
338-
" String __target__ = Utils;\n" +
338+
" return Utils;\n" +
339339
" }\n" +
340340
"}" );
341341
}
@@ -358,10 +358,10 @@ protected void withMapperWithoutImports(String attribute) {
358358
"abstract class CarMapperImpl\n" +
359359
" implements CarMapper {\n" +
360360
"\n" +
361-
" void __test__(\n" +
361+
" String __test__(\n" +
362362
" Car car\n" +
363363
" ) {\n" +
364-
" String __target__ = Collections;\n" +
364+
" return Collections;\n" +
365365
" }\n" +
366366
"}" );
367367

@@ -385,11 +385,11 @@ protected void withMultiSourceParameters(String attribute) {
385385
"abstract class CarMapperImpl\n" +
386386
" implements CarMapper {\n" +
387387
"\n" +
388-
" void __test__(\n" +
388+
" String __test__(\n" +
389389
" Car car,\n" +
390390
" String make\n" +
391391
" ) {\n" +
392-
" String __target__ = car.;\n" +
392+
" return car.;\n" +
393393
" }\n" +
394394
"}" );
395395
}
@@ -414,10 +414,10 @@ protected void withGenericSourceParameters(String attribute) {
414414
"abstract class CarMapperImpl\n" +
415415
" implements CarMapper {\n" +
416416
"\n" +
417-
" void __test__(\n" +
417+
" String __test__(\n" +
418418
" Wrapper<Car> carWrapper\n" +
419419
" ) {\n" +
420-
" String __target__ = carWrapper.getValue().;\n" +
420+
" return carWrapper.getValue().;\n" +
421421
" }\n" +
422422
"}" );
423423
}
@@ -442,11 +442,11 @@ protected void withExpressionWithSourceParameterWithAnnotations(String attribute
442442
"abstract class CarMapperImpl\n" +
443443
" implements CarMapper {\n" +
444444
"\n" +
445-
" void __test__(\n" +
445+
" String __test__(\n" +
446446
" @Context\n" +
447447
" Wrapper<Car> carWrapper\n" +
448448
" ) {\n" +
449-
" String __target__ = carWrapper.getValue().;\n" +
449+
" return carWrapper.getValue().;\n" +
450450
" }\n" +
451451
"}" );
452452
}
@@ -472,10 +472,10 @@ protected void withExpressionWithSourceParameterWithMultipleGenerics(String attr
472472
"abstract class CarMapperImpl\n" +
473473
" implements CarMapper {\n" +
474474
"\n" +
475-
" void __test__(\n" +
475+
" String __test__(\n" +
476476
" Wrapper<BiFunction<String, Number, Car>> carWrapper\n" +
477477
" ) {\n" +
478-
" String __target__ = carWrapper.getValue().apply(null, null).;\n" +
478+
" return carWrapper.getValue().apply(null, null).;\n" +
479479
" }\n" +
480480
"}" );
481481
}
@@ -498,10 +498,10 @@ protected void withExpressionWithGenericMethod(String attribute) {
498498
"abstract class CarMapperImpl\n" +
499499
" implements CarMapper {\n" +
500500
"\n" +
501-
" <T extends Number> void __test__(\n" +
501+
" <T extends Number> int __test__(\n" +
502502
" NumberWrapper<T> numberWrapper\n" +
503503
" ) {\n" +
504-
" int __target__ = numberWrapper.;\n" +
504+
" return numberWrapper.;\n" +
505505
" }\n" +
506506
"}" );
507507
}
@@ -524,11 +524,11 @@ protected void withGenericMapper(String attribute) {
524524
"abstract class CarMapperImpl<T, R>\n" +
525525
" implements CarMapper<T, R> {\n" +
526526
"\n" +
527-
" void __test__(\n" +
527+
" String __test__(\n" +
528528
" Car car,\n" +
529529
" String make\n" +
530530
" ) {\n" +
531-
" String __target__ = car.;\n" +
531+
" return car.;\n" +
532532
" }\n" +
533533
"}" );
534534
}
@@ -559,10 +559,10 @@ protected void withClassMapper(String attribute) {
559559
"abstract class CarMapperImpl\n" +
560560
" extends CarMapper {\n" +
561561
"\n" +
562-
" void __test__(\n" +
562+
" String __test__(\n" +
563563
" Car car\n" +
564564
" ) {\n" +
565-
" String __target__ = car.;\n" +
565+
" return car.;\n" +
566566
" }\n" +
567567
"}" );
568568
}
@@ -594,10 +594,10 @@ protected void withTargetUsingStaticString(String attribute) {
594594
"abstract class CarMapperImpl\n" +
595595
" extends CarMapper {\n" +
596596
"\n" +
597-
" void __test__(\n" +
597+
" String __test__(\n" +
598598
" Car car\n" +
599599
" ) {\n" +
600-
" String __target__ = car.;\n" +
600+
" return car.;\n" +
601601
" }\n" +
602602
"}" );
603603
}

0 commit comments

Comments
 (0)