IDEA-179490 "Can be replaced with single expression in functional style" in Java fails when field name matches class name

This commit is contained in:
Tagir Valeev
2017-09-26 09:44:21 +07:00
parent fa6bbd0b5d
commit b2df7a760a
4 changed files with 47 additions and 7 deletions
@@ -32,6 +32,8 @@ import com.intellij.psi.impl.PsiDiamondTypeUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ObjectUtils;
import com.siyeh.ig.psiutils.BoolUtils;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ControlFlowUtils;
@@ -206,10 +208,8 @@ public class OptionalIsPresentInspection extends BaseJavaBatchLocalInspectionToo
PsiType type = optionalVariable.getType();
JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(trueValue.getProject());
SuggestedNameInfo info = javaCodeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, type);
if (info.names.length == 0) {
info = javaCodeStyleManager.suggestVariableName(VariableKind.PARAMETER, "value", null, type);
}
String paramName = javaCodeStyleManager.suggestUniqueVariableName(info, trueValue, true).names[0];
String baseName = ObjectUtils.coalesce(ArrayUtil.getFirstElement(info.names), "value");
String paramName = javaCodeStyleManager.suggestUniqueVariableName(baseName, trueValue, true);
if(trueValue instanceof PsiExpressionStatement) {
trueValue = ((PsiExpressionStatement)trueValue).getExpression();
}
@@ -179,7 +179,8 @@ public abstract class JavaCodeStyleManager {
public abstract String propertyNameToVariableName(@NonNls @NotNull String propertyName, @NotNull VariableKind variableKind);
/**
* Suggests a unique name for the variable used at the specified location.
* Suggests a unique name for the variable used at the specified location. The returned name is guaranteed to not shadow
* the existing name.
*
* @param baseName the base name for the variable.
* @param place the location where the variable will be used.
@@ -190,12 +191,13 @@ public abstract class JavaCodeStyleManager {
public abstract String suggestUniqueVariableName(@NonNls @NotNull String baseName, PsiElement place, boolean lookForward);
/**
* Suggests a unique name for the variable used at the specified location.
* Suggests a unique names for the variable used at the specified location. The resulting name info may contain names which
* shadow existing names.
*
* @param baseNameInfo the base name info for the variable.
* @param place the location where the variable will be used.
* @param lookForward if true, the existing variables are searched in both directions; if false - only backward
* @return the generated unique name
* @return the generated unique name info.
*/
@NotNull
public SuggestedNameInfo suggestUniqueVariableName(@NotNull SuggestedNameInfo baseNameInfo,
@@ -0,0 +1,18 @@
// "Replace Optional.isPresent() condition with functional style expression" "true"
import java.util.Optional;
public class Test {
private Power power;
interface Power {
static Optional<Power> parseValue(String value) {
return Optional.empty();
}
}
void testOpt(String powerValue) {
Optional<Power> optPower = Power.parseValue(powerValue);
optPower.ifPresent(power1 -> power = power1);
}
}
@@ -0,0 +1,20 @@
// "Replace Optional.isPresent() condition with functional style expression" "true"
import java.util.Optional;
public class Test {
private Power power;
interface Power {
static Optional<Power> parseValue(String value) {
return Optional.empty();
}
}
void testOpt(String powerValue) {
Optional<Power> optPower = Power.parseValue(powerValue);
if (optPower<caret>.isPresent()) {
power = optPower.get();
}
}
}