[groovy] unnecessary def: offer to remove modifier when other

modifiers are present even if there are generics
This commit is contained in:
Daniil Ovchinnikov
2016-11-21 15:23:20 +03:00
parent 78b5ceb2e7
commit 6d9af3466b
3 changed files with 15 additions and 4 deletions
@@ -25,12 +25,13 @@ import org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionBundle;
import org.jetbrains.plugins.groovy.codeInspection.GroovySuppressableInspectionTool;
import org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix;
import org.jetbrains.plugins.groovy.codeInspection.bugs.GrRemoveModifierFix;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifier;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList;
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtilKt;
public class GrUnnecessaryDefModifierInspection extends GroovySuppressableInspectionTool implements CleanupLocalInspectionTool{
import static org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes.kDEF;
public class GrUnnecessaryDefModifierInspection extends GroovySuppressableInspectionTool implements CleanupLocalInspectionTool {
private static final GrModifierFix FIX = new GrRemoveModifierFix(GrModifier.DEF);
@@ -40,13 +41,13 @@ public class GrUnnecessaryDefModifierInspection extends GroovySuppressableInspec
return new PsiElementVisitor() {
@Override
public void visitElement(PsiElement modifier) {
if (modifier.getNode().getElementType() != GroovyTokenTypes.kDEF) return;
if (modifier.getNode().getElementType() != kDEF) return;
PsiElement list = modifier.getParent();
if (!(list instanceof GrModifierList)) return;
PsiElement owner = list.getParent();
if (!PsiUtilKt.modifierListMayBeEmpty(owner)) return;
if (!PsiUtilKt.modifierListMayBeEmpty(owner) && !PsiUtilKt.hasOtherModifiers((GrModifierList)list, kDEF)) return;
holder.registerProblem(
modifier,
@@ -16,7 +16,9 @@
package org.jetbrains.plugins.groovy.lang.psi.util
import com.intellij.psi.PsiElement
import com.intellij.psi.tree.IElementType
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes.mCOLON
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration
import org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause
@@ -46,3 +48,7 @@ fun modifierListMayBeEmpty(owner: PsiElement?): Boolean = when (owner) {
is GrVariableDeclaration -> owner.typeElementGroovy != null
else -> true
}
fun GrModifierList.hasOtherModifiers(modifierElementType: IElementType): Boolean {
return modifiers.any { it.node.elementType != modifierElementType }
}
@@ -37,6 +37,8 @@ def baw(<warning descr="Modifier 'def' is not necessary">def</warning> a) {}
def <T> T bax() {}
synchronized <warning descr="Modifier 'def' is not necessary">def</warning> <T> T baxx(Class<T> c) {}
<warning descr="Modifier 'def' is not necessary">def</warning> Object bar
def baf
@@ -64,6 +66,8 @@ boolean baz(a) {}
def <T> T bax() {}
synchronized <T> T baxx(Class<T> c) {}
Object bar
def baf