Java: update "Mismatched query and update of 'StringBuilder'" inspection for String Templates (IDEA-336330)

GitOrigin-RevId: 5eb30ccc1e84effaf5eb7d3767f64017b3a338a4
This commit is contained in:
Bas Leijdekkers
2023-10-26 14:18:21 +02:00
committed by intellij-monorepo-bot
parent 758512f65d
commit 5d6f4a3258
3 changed files with 18 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.bugs;
import com.intellij.codeInsight.daemon.impl.UnusedSymbolUtil;
@@ -290,21 +290,19 @@ public class MismatchedStringBuilderQueryUpdateInspection extends BaseInspection
if (queried) return;
super.visitReferenceExpression(expression);
final PsiElement parent = ParenthesesUtils.getParentSkipParentheses(expression);
if (!(parent instanceof PsiPolyadicExpression polyadicExpression)) {
return;
if (parent instanceof PsiPolyadicExpression polyadicExpression) {
final IElementType tokenType = polyadicExpression.getOperationTokenType();
if (!JavaTokenType.PLUS.equals(tokenType) || !ExpressionUtils.hasStringType(polyadicExpression)) {
return;
}
}
final IElementType tokenType = polyadicExpression.getOperationTokenType();
if (!JavaTokenType.PLUS.equals(tokenType)) {
else if (!(parent instanceof PsiTemplate)) {
return;
}
final PsiElement target = expression.resolve();
if (!variable.equals(target)) {
return;
}
final PsiType type = polyadicExpression.getType();
if (type == null || !type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
return;
}
queried = true;
}

View File

@@ -208,4 +208,13 @@ class Sample {
private final static StringBuilder <warning descr="Contents of 'StringBuilder sbField' are updated, but never queried">sbField</warning> = ((new StringBuilder())
.append("asdf"));
String foo(List<CharSequence> list, boolean is) {
var sb = new StringBuilder();
if (is) {
sb.append("is");
}
return STR."\{sb} \{list.stream().filter(t -> t.equals("s")).collect(java.util.stream.Collectors.joining(","))}";
}
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.bugs;
import com.intellij.codeInspection.InspectionProfileEntry;
@@ -25,7 +25,7 @@ public class MismatchedStringBuilderQueryUpdateInspectionTest extends LightJavaI
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JAVA_LATEST_WITH_LATEST_JDK; // need switch expressions
return JAVA_21;
}
@Override