IDEA-80590 (hint to ignore unused for-each parameters and resource variables)

This commit is contained in:
Roman Shevchenko
2012-03-28 00:15:10 +02:00
parent 465c18395e
commit 2a99d489af
8 changed files with 97 additions and 29 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -75,6 +75,7 @@ import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.OverridingMethodsSearch;
import com.intellij.psi.search.searches.SuperMethodsSearch;
import com.intellij.psi.util.PropertyUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.refactoring.changeSignature.ChangeSignatureGestureDetector;
import com.intellij.util.Processor;
@@ -322,16 +323,18 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
return null;
}
@Nullable
private HighlightInfo processLocalVariable(PsiLocalVariable variable, ProgressIndicator progress) {
PsiIdentifier identifier = variable.getNameIdentifier();
if (identifier == null) return null;
if (variable instanceof PsiResourceVariable && PsiUtil.isIgnoredName(variable.getName())) return null;
if (isImplicitUsage(variable, progress)) return null;
if (!myRefCountHolder.isReferenced(variable)) {
String message = JavaErrorMessages.message("local.variable.is.never.used", identifier.getText());
HighlightInfo highlightInfo = createUnusedSymbolInfo(identifier, message, HighlightInfoType.UNUSED_SYMBOL);
QuickFixAction.registerQuickFixAction(highlightInfo, new RemoveUnusedVariableFix(variable), myUnusedSymbolKey);
IntentionAction fix = variable instanceof PsiResourceVariable ? new RenameToIgnoredFix(variable) : new RemoveUnusedVariableFix(variable);
QuickFixAction.registerQuickFixAction(highlightInfo, fix, myUnusedSymbolKey);
return highlightInfo;
}
@@ -356,7 +359,6 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
return null;
}
public static boolean isImplicitUsage(final PsiModifierListOwner element, ProgressIndicator progress) {
if (UnusedSymbolLocalInspection.isInjected(element)) return true;
for (ImplicitUsageProvider provider : ourImplicitUsageProviders) {
@@ -506,10 +508,10 @@ public class PostHighlightingPass extends TextEditorHighlightingPass {
}
}
}
else if (declarationScope instanceof PsiForeachStatement && !Comparing.strEqual(parameter.getName(), "ignore")) {
else if (declarationScope instanceof PsiForeachStatement && !PsiUtil.isIgnoredName(parameter.getName())) {
HighlightInfo highlightInfo = checkUnusedParameter(parameter, progress);
if (highlightInfo != null) {
QuickFixAction.registerQuickFixAction(highlightInfo, new EmptyIntentionAction(UnusedSymbolLocalInspection.DISPLAY_NAME), myUnusedSymbolKey);
QuickFixAction.registerQuickFixAction(highlightInfo, new RenameToIgnoredFix(parameter), myUnusedSymbolKey);
return highlightInfo;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -799,7 +799,7 @@ public class HighlightMethodUtil {
if (aClass != null) {
String className = aClass instanceof PsiAnonymousClass ? null : aClass.getName();
if (!Comparing.strEqual(methodName, className)) {
if (className != null && !Comparing.strEqual(methodName, className)) {
errorResult = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, method.getNameIdentifier(),
JavaErrorMessages.message("missing.return.type"));
QuickFixAction.registerQuickFixAction(errorResult, new RenameElementFix(method, className));
@@ -0,0 +1,25 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.psi.PsiNamedElement;
import org.jetbrains.annotations.NotNull;
public class RenameToIgnoredFix extends RenameElementFix {
public RenameToIgnoredFix(@NotNull PsiNamedElement element) {
super(element, "ignored");
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@ import java.util.*;
public final class PsiUtil extends PsiUtilCore {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.util.PsiUtil");
public static final int ACCESS_LEVEL_PUBLIC = 4;
public static final int ACCESS_LEVEL_PROTECTED = 3;
public static final int ACCESS_LEVEL_PACKAGE_LOCAL = 2;
@@ -652,7 +653,7 @@ public final class PsiUtil extends PsiUtilCore {
@NotNull
public static List<PsiTypeElement> getParameterTypeElements(@NotNull PsiParameter parameter) {
PsiTypeElement typeElement = parameter.getTypeElement();
return typeElement.getType() instanceof PsiDisjunctionType
return typeElement != null && typeElement.getType() instanceof PsiDisjunctionType
? PsiTreeUtil.getChildrenOfTypeAsList(typeElement, PsiTypeElement.class)
: Collections.singletonList(typeElement);
}
@@ -839,7 +840,7 @@ public final class PsiUtil extends PsiUtilCore {
return hasDefaultCtrInHierarchy(clazz, allowProtected, checkModifiers, null);
}
private static boolean hasDefaultCtrInHierarchy(PsiClass clazz, boolean allowProtected, boolean checkModifiers, Set<PsiClass> visited) {
private static boolean hasDefaultCtrInHierarchy(PsiClass clazz, boolean allowProtected, boolean checkModifiers, @Nullable Set<PsiClass> visited) {
final PsiMethod[] constructors = clazz.getConstructors();
if (constructors.length > 0) {
for (PsiMethod cls: constructors) {
@@ -928,4 +929,8 @@ public final class PsiUtil extends PsiUtilCore {
public static boolean isCatchParameter(@Nullable final PsiElement element) {
return element instanceof PsiParameter && element.getParent() instanceof PsiCatchSection;
}
public static boolean isIgnoredName(@Nullable final String name) {
return "ignore".equals(name) || "ignored".equals(name);
}
}
@@ -1,5 +1,22 @@
class UnusedDeclBug {
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.*;
import java.net.*;
class UnusedDeclBug {
public static enum Concern {
// These fields are used! Just because I don't mention them by name
// doesn't mean they aren't used!
@@ -29,4 +46,22 @@ class ForEachTest {
}
System.out.println(count);
}
}
class TryWithResourcesTest {
public static void main(String[] args) {
System.out.println(checkUrl("bad url"));
}
private static String checkUrl(String url) {
try {
URLConnection connection = new URL(url).openConnection();
try (InputStream ignored = connection.getInputStream()) {
return connection.getURL().toString();
}
}
catch (IOException e) {
return null;
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.CodeInsightBundle;
@@ -29,29 +28,33 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiNamedElement;
import com.intellij.refactoring.rename.RenameProcessor;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author ven
*/
public class RenameElementFix implements IntentionAction, LocalQuickFix {
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.RenameFileFix");
private final PsiNamedElement myElement;
private final String myNewName;
private final String myText;
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.RenameFileFix");
public RenameElementFix(PsiNamedElement aClass) {
myElement = aClass;
myNewName = myElement.getContainingFile().getVirtualFile().getNameWithoutExtension();
public RenameElementFix(@NotNull PsiNamedElement element) {
myElement = element;
final VirtualFile vFile = myElement.getContainingFile().getVirtualFile();
assert vFile != null : element;
myNewName = vFile.getNameWithoutExtension();
myText = CodeInsightBundle.message("rename.public.class.text", myElement.getName(), myNewName);
}
public RenameElementFix(PsiNamedElement element, String newName) {
public RenameElementFix(@NotNull PsiNamedElement element, @NotNull String newName) {
myElement = element;
myNewName = newName;
myText = CodeInsightBundle.message("rename.named.element.text", myElement.getName(), myNewName);
@@ -72,7 +75,7 @@ public class RenameElementFix implements IntentionAction, LocalQuickFix {
@Override
@NotNull
public String getFamilyName() {
return CodeInsightBundle.message("rename.public.class.family");
return CodeInsightBundle.message("rename.element.family");
}
@Override
@@ -89,12 +92,12 @@ public class RenameElementFix implements IntentionAction, LocalQuickFix {
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
public boolean isAvailable(@NotNull Project project, @Nullable Editor editor, PsiFile file) {
if (!myElement.isValid()) {
return false;
}
final NamesValidator namesValidator = LanguageNamesValidation.INSTANCE.forLanguage(file.getLanguage());
return namesValidator.isIdentifier(myNewName, project);
return namesValidator != null && namesValidator.isIdentifier(myNewName, project);
}
@Override
@@ -468,7 +468,7 @@ create.directory.text=Create Directory {0}
create.file.text=Create File {0}
create.tagfile.text=Create Tag File {0}
rename.file.fix=Rename File
rename.public.class.family=Rename Public Class
rename.element.family=Rename Element
rename.public.class.text=Rename class ''{0}'' to ''{1}''
rename.named.element.text=Rename ''{0}'' to ''{1}''
dialog.edit.template.checkbox.html.text=HTML Text
@@ -19,6 +19,7 @@ import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
@@ -153,11 +154,8 @@ public class EmptyCatchBlockInspection extends BaseInspection {
if (identifier == null) {
return;
}
@NonNls final String parameterName =
parameter.getName();
if (m_ignoreIgnoreParameter &&
("ignore".equals(parameterName) ||
"ignored".equals(parameterName))) {
@NonNls final String parameterName = parameter.getName();
if (m_ignoreIgnoreParameter && PsiUtil.isIgnoredName(parameterName)) {
return;
}
final PsiElement catchToken = section.getFirstChild();