mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
[java-inspections] TrivialFunctionalExpressionUsageInspection: solve naming conflicts instead of keeping the block
Keeping the block does not work correctly for blocks with return statement GitOrigin-RevId: d14e22e793840d74957928a8d7748275db4929ef
This commit is contained in:
committed by
intellij-monorepo-bot
parent
e536db53dc
commit
4c29ff02f6
@@ -38,4 +38,15 @@ class X {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> getStrings(List<String> test) {
|
||||
ArrayList<String> objects1 = new ArrayList<>();
|
||||
objects1.add("1");
|
||||
ArrayList<String> strings = objects1;
|
||||
for (String string : test) {
|
||||
System.out.println("1");
|
||||
strings.add(string);
|
||||
}
|
||||
return strings;
|
||||
}
|
||||
}
|
||||
@@ -37,4 +37,16 @@ class X {
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static List<String> getStrings(List<String> test) {
|
||||
return test.stream()
|
||||
.collect(()->{
|
||||
ArrayList<String> objects = new ArrayList<>();
|
||||
objects.add("1");
|
||||
return objects;
|
||||
}, (strings, string) -> {
|
||||
System.out.println("1");
|
||||
strings.add(string);
|
||||
}, (strings, strings2) -> strings.addAll(strings2));
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,7 @@
|
||||
|
||||
class Test {
|
||||
void test() {
|
||||
{
|
||||
int a = 6;
|
||||
}
|
||||
int a1 =6;
|
||||
int a =5;
|
||||
}
|
||||
}
|
||||
@@ -241,14 +241,10 @@ public class TrivialFunctionalExpressionUsageInspection extends AbstractBaseJava
|
||||
PsiElement anchor = result.getAnchor();
|
||||
statement = ObjectUtils.tryCast(statements[statements.length - 1], PsiReturnStatement.class);
|
||||
PsiElement gParent = anchor.getParent();
|
||||
if (hasNameConflict(statements, anchor, element)) {
|
||||
gParent.addBefore(JavaPsiFacade.getElementFactory(element.getProject()).createStatementFromText(ct.text(body), anchor), anchor);
|
||||
}
|
||||
else {
|
||||
for (PsiElement child = body.getFirstChild(); child != null; child = child.getNextSibling()) {
|
||||
if (child != statement && !(child instanceof PsiJavaToken)) {
|
||||
gParent.addBefore(ct.markUnchanged(child), anchor);
|
||||
}
|
||||
solveNameConflicts(statements, anchor, element);
|
||||
for (PsiElement child = body.getFirstChild(); child != null; child = child.getNextSibling()) {
|
||||
if (child != statement && !(child instanceof PsiJavaToken)) {
|
||||
gParent.addBefore(ct.markUnchanged(child), anchor);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -266,15 +262,24 @@ public class TrivialFunctionalExpressionUsageInspection extends AbstractBaseJava
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasNameConflict(PsiStatement[] statements, PsiElement anchor, PsiLambdaExpression lambda) {
|
||||
private static void solveNameConflicts(PsiStatement[] statements, @NotNull PsiElement anchor, @NotNull PsiLambdaExpression lambda) {
|
||||
Predicate<PsiVariable> allowedVar = variable -> PsiTreeUtil.isAncestor(lambda, variable, true);
|
||||
JavaCodeStyleManager manager = JavaCodeStyleManager.getInstance(anchor.getProject());
|
||||
return StreamEx.of(statements).select(PsiDeclarationStatement.class)
|
||||
Project project = anchor.getProject();
|
||||
JavaCodeStyleManager manager = JavaCodeStyleManager.getInstance(project);
|
||||
StreamEx.of(statements).select(PsiDeclarationStatement.class)
|
||||
.flatArray(PsiDeclarationStatement::getDeclaredElements)
|
||||
.select(PsiNamedElement.class)
|
||||
.map(PsiNamedElement::getName)
|
||||
.nonNull()
|
||||
.anyMatch(name -> !name.equals(manager.suggestUniqueVariableName(name, anchor, allowedVar)));
|
||||
.select(PsiVariable.class)
|
||||
.forEach(e -> {
|
||||
PsiIdentifier identifier = e.getNameIdentifier();
|
||||
if (identifier == null) return;
|
||||
String name = identifier.getText();
|
||||
String newName = manager.suggestUniqueVariableName(name, anchor, allowedVar);
|
||||
if (!name.equals(newName)) {
|
||||
List<PsiReferenceExpression> refs = VariableAccessUtils.getVariableReferences(e, anchor);
|
||||
refs.forEach(ref -> ref.handleElementRename(newName));
|
||||
identifier.replace(JavaPsiFacade.getElementFactory(project).createIdentifier(newName));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void inlineCallArguments(PsiMethodCallExpression callExpression,
|
||||
|
||||
Reference in New Issue
Block a user