diff --git a/java/java-analysis-impl/src/com/siyeh/ig/bugs/MismatchedStringBuilderQueryUpdateInspection.java b/java/java-analysis-impl/src/com/siyeh/ig/bugs/MismatchedStringBuilderQueryUpdateInspection.java index b2f16acadcdd..54b447ab78a6 100644 --- a/java/java-analysis-impl/src/com/siyeh/ig/bugs/MismatchedStringBuilderQueryUpdateInspection.java +++ b/java/java-analysis-impl/src/com/siyeh/ig/bugs/MismatchedStringBuilderQueryUpdateInspection.java @@ -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; } diff --git a/java/java-tests/testData/ig/com/siyeh/igtest/bugs/mismatched_string_builder_query_update/MismatchedStringBuilderQueryUpdate.java b/java/java-tests/testData/ig/com/siyeh/igtest/bugs/mismatched_string_builder_query_update/MismatchedStringBuilderQueryUpdate.java index 165866917ea2..b61c0e5793e8 100644 --- a/java/java-tests/testData/ig/com/siyeh/igtest/bugs/mismatched_string_builder_query_update/MismatchedStringBuilderQueryUpdate.java +++ b/java/java-tests/testData/ig/com/siyeh/igtest/bugs/mismatched_string_builder_query_update/MismatchedStringBuilderQueryUpdate.java @@ -208,4 +208,13 @@ class Sample { private final static StringBuilder sbField = ((new StringBuilder()) .append("asdf")); + String foo(List 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(","))}"; + } } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/siyeh/ig/bugs/MismatchedStringBuilderQueryUpdateInspectionTest.java b/java/java-tests/testSrc/com/siyeh/ig/bugs/MismatchedStringBuilderQueryUpdateInspectionTest.java index c67dd6873043..6434597c0b06 100644 --- a/java/java-tests/testSrc/com/siyeh/ig/bugs/MismatchedStringBuilderQueryUpdateInspectionTest.java +++ b/java/java-tests/testSrc/com/siyeh/ig/bugs/MismatchedStringBuilderQueryUpdateInspectionTest.java @@ -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