IDEA-107277 (False positive for "Unecessary 'this' qualifier" inspection)

This commit is contained in:
Bas Leijdekkers
2013-07-11 15:34:21 +02:00
parent 6842d0073f
commit cce9f7f400
4 changed files with 138 additions and 108 deletions
@@ -22,7 +22,6 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
@@ -72,11 +71,9 @@ public class UnnecessaryThisInspection extends BaseInspection {
}
@Override
public void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
public void doFix(Project project, ProblemDescriptor descriptor) {
final PsiElement thisToken = descriptor.getPsiElement();
final PsiReferenceExpression thisExpression =
(PsiReferenceExpression)thisToken.getParent();
final PsiReferenceExpression thisExpression = (PsiReferenceExpression)thisToken.getParent();
assert thisExpression != null;
final String newExpression = thisExpression.getReferenceName();
if (newExpression == null) {
@@ -156,7 +153,7 @@ public class UnnecessaryThisInspection extends BaseInspection {
final PsiMethod[] methods = parentClass.findMethodsByName(methodName, true);
for (PsiMethod method : methods) {
final PsiClass containingClass = method.getContainingClass();
if (resolveHelper.isAccessible(method, expression, containingClass)) {
if (resolveHelper.isAccessible(method, expression, null)) {
if (method.hasModifierProperty(PsiModifier.PRIVATE) && !PsiTreeUtil.isAncestor(containingClass, expression, true)) {
continue;
}
@@ -1,72 +0,0 @@
package com.siyeh.igtest.style.unnecessary_this;
public class UnnecessaryThisInspection
{
private int m_foo;
public void fooBar()
{
this.m_foo = 3;
}
class X {
private int m_foo;
public void fooBar() {};
void foo() {
UnnecessaryThisInspection.this.m_foo = 4;
UnnecessaryThisInspection.this.fooBar();
}
}
public void fooBaz( int m_foo)
{
this.m_foo = 3;
}
public void fooBarangus()
{
int m_foo;
this.m_foo = 3;
}
public void fooBarzoom()
{
for(int m_foo = 0;m_foo<4; m_foo++)
{
this.m_foo = 3;
}
this.fooBar();
}
private Throwable throwable = null;
public void method()
{
try
{
}
catch (Throwable throwable)
{
this.throwable = throwable;
throwable.printStackTrace();
}
}
public void foo(String s) {}
class D{
private void foo(String s) {}
}
class C extends D {
class Box {
void bar() {
UnnecessaryThisInspection.this.foo("");
}
}
}
}
@@ -1,25 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>UnnecessaryThisInspection.java</file>
<line>40</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary 'this' qualifier</problem_class>
<description>&lt;code&gt;this&lt;/code&gt; is unnecessary in this context #loc</description>
</problem>
<problem>
<file>UnnecessaryThisInspection.java</file>
<line>9</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary 'this' qualifier</problem_class>
<description>&lt;code&gt;this&lt;/code&gt; is unnecessary in this context #loc</description>
</problem>
<problem>
<file>UnnecessaryThisInspection.java</file>
<line>68</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary 'this' qualifier</problem_class>
<description>&lt;code&gt;UnnecessaryThisInspection.this&lt;/code&gt; is unnecessary in this context #loc</description>
</problem>
</problems>
@@ -1,11 +1,141 @@
package com.siyeh.ig.style;
import com.siyeh.ig.IGInspectionTestCase;
import com.intellij.codeInspection.LocalInspectionTool;
import com.siyeh.ig.LightInspectionTestCase;
public class UnnecessaryThisInspectionTest extends IGInspectionTestCase {
public class UnnecessaryThisInspectionTest extends LightInspectionTestCase {
public void test() throws Exception {
doTest("com/siyeh/igtest/style/unnecessary_this",
new UnnecessaryThisInspection());
public void testSimpleField() {
doTest("class A {" +
" private int x;" +
" void m() {" +
" /*'this' is unnecessary in this context*/this/**/.x = 3;" +
" }" +
"}");
}
public void testSimpleMethod() {
doTest("class A {" +
" void x() {}" +
" void m() {" +
" /*'this' is unnecessary in this context*/this/**/.x();" +
" }" +
"}");
}
public void testQualifiedThisNeeded() {
doTest("class A {" +
" public void foo(String s) {}" +
" class D{" +
" public void foo(String s) {}" +
" }" +
" class C extends D {" +
" class Box {" +
" void bar() {" +
" A.this.foo(\"\");" +
" }" +
" }" +
" }" +
"}");
}
public void testQualifiedThisNeeded2() {
doTest("class A {" +
" private int x;" +
" public void x() {}" +
" class X {" +
" private int x;" +
" public void x(){}" +
" void y() {" +
" A.this.x = 4;" +
" A.this.x();" +
" }" +
" }" +
"}");
}
public void testQualifiedThisNotNeeded() {
doTest("class A {" +
" public void foo(String s) {}" +
" class D{" +
" private void foo(String s) {}" +
" }" +
" class C extends D {" +
" class Box {" +
" void bar() {" +
" /*'A.this' is unnecessary in this context*/A.this/**/.foo(\"\");" +
" }" +
" }" +
" }" +
"}");
}
public void testQualifiedThisDifferentPackage() {
myFixture.addClass("package foo;" +
"public abstract class Foo {" +
" protected void foo() {}" +
"}");
doTest("package bar;" +
"import foo.Foo;" +
"final class Bar extends Foo {" +
" public void foo() {}" +
" final class InnerBar extends Foo {" +
" void bar() {" +
" Bar.this.foo(); " +
" }" +
" }" +
"}");
}
/**
* IDEA-42154
*/
public void testCatchBlockParameter() {
doTest("class A {" +
" private Throwable throwable = null;" +
" public void method() {" +
" try {" +
" } catch (Throwable throwable) {" +
" this.throwable = throwable;" +
" throwable.printStackTrace();" +
" }" +
" }" +
"}");
}
public void testForLoopParameter() {
doTest("class A {" +
" private int i = 0;" +
" void m() {" +
" for (int i = 0; i < 10; i++) {" +
" this.i = 3;" +
" }" +
" }" +
"}");
}
public void testMethodParameter() {
doTest("class A {" +
" private int i = 1;" +
" void m(int i) {" +
" this.i = 3;" +
" }" +
"}");
}
public void testLocalVariable() {
doTest("class A {" +
" private int i = 2;" +
" void foo() {" +
" int i = 3;" +
" this.i=4;" +
" }" +
"}");
}
@Override
protected LocalInspectionTool getInspection() {
return new UnnecessaryThisInspection();
}
}