redundant cast: restoring call by top level, let's search by arg list offset in order to avoid finding method calls from qualifiers

This commit is contained in:
Anna.Kozlova
2016-04-20 17:44:56 +02:00
parent 22c8d0f9ce
commit 401ec5dbfa
4 changed files with 26 additions and 5 deletions

View File

@@ -361,7 +361,7 @@ public class RedundantCastUtil {
final PsiCall call = LambdaUtil.treeWalkUp(expression);
if (call != null) {
final PsiCall callCopy = (PsiCall)call.copy();
newCall = PsiTreeUtil.getParentOfType(callCopy.findElementAt(expression.getTextRange().getStartOffset() - call.getTextRange().getStartOffset()), expression.getClass());
newCall = PsiTreeUtil.getParentOfType(callCopy.findElementAt(argumentList.getTextRange().getStartOffset() - call.getTextRange().getStartOffset()), expression.getClass());
}
else {
newCall = (PsiCall)expression.copy();

View File

@@ -169,11 +169,12 @@ public class PsiDiamondTypeUtil {
copy = initializer.getInitializers()[0].replace(expression);
}
else {
final int offset = expression.getTextRange().getStartOffset();
final PsiExpressionList argumentList = expression.getArgumentList();
final int offset = (argumentList != null ? argumentList : expression).getTextRange().getStartOffset();
final PsiCall call = LambdaUtil.treeWalkUp(expression);
if (call instanceof PsiCallExpression) { //exclude EnumConstant
final PsiCall callCopy = (PsiCall)call.copy();
copy = callCopy.findElementAt(offset - call.getTextRange().getStartOffset());
if (call != null) {
final PsiCall callCopy = LambdaUtil.copyTopLevelCall(call);
copy = callCopy != null ? callCopy.findElementAt(offset - call.getTextRange().getStartOffset()) : null;
}
else {
final PsiFile containingFile = expression.getContainingFile();

View File

@@ -0,0 +1,16 @@
class Test {
{
setFont(getFont().substring((int)fontSize()));
}
void setFont(String s) {}
String getFont() {
return "";
}
double fontSize () {
return 1.0;
}
}

View File

@@ -75,6 +75,10 @@ public class LambdaRedundantCastTest extends LightDaemonAnalyzerTestCase {
public void testIDEA154861() { doTest();}
public void testQualifierOfMethodWithCast() throws Exception {
doTest();
}
private void doTest() {
doTest(BASE_PATH + "/" + getTestName(false) + ".java", true, false);
}