diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index c002a11088b1..8ebb21229b1c 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -760,6 +760,10 @@ + #ref() overrides varargs method #loc +negatively.named.boolean.variable.problem.descriptor=Boolean variable #ref is negatively named #loc +negatively.named.boolean.variable.display.name=Negatively named boolean variable invert.quickfix.family.name=Invert boolean invert.method.quickfix=Invert method invert.quickfix=Invert ''{0}'' diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/NegativelyNamedBooleanVariableInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/NegativelyNamedBooleanVariableInspectionBase.java new file mode 100644 index 000000000000..0d3ba0321c12 --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/NegativelyNamedBooleanVariableInspectionBase.java @@ -0,0 +1,72 @@ +/* + * Copyright 2000-2013 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.siyeh.ig.dataflow; + +import com.intellij.psi.PsiType; +import com.intellij.psi.PsiVariable; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspection; +import com.siyeh.ig.BaseInspectionVisitor; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; + +/** + * @author Bas Leijdekkers + */ +public class NegativelyNamedBooleanVariableInspectionBase extends BaseInspection { + + @NonNls + private static final String[] NEGATIVE_NAMES = {"not", "isNot", "shouldNot", "shallNot", "willNot", "cannot", "cant", "hasNot", + "couldNot", "hidden", "isHidden", "disabled", "isDisabled"}; + + @Nls + @NotNull + @Override + public String getDisplayName() { + return InspectionGadgetsBundle.message("negatively.named.boolean.variable.display.name"); + } + + @NotNull + @Override + protected String buildErrorString(Object... infos) { + return InspectionGadgetsBundle.message("negatively.named.boolean.variable.problem.descriptor"); + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new NegativelyNamedBooleanVariableVisitor(); + } + + private static class NegativelyNamedBooleanVariableVisitor extends BaseInspectionVisitor { + + @Override + public void visitVariable(PsiVariable variable) { + super.visitVariable(variable); + if (!PsiType.BOOLEAN.equals(variable.getType())) { + return; + } + final String name = variable.getName(); + for (final String negativeName : NEGATIVE_NAMES) { + if (name.startsWith(negativeName) && + (name.length() == negativeName.length() || Character.isUpperCase(name.charAt(negativeName.length())))) { + registerVariableError(variable, variable); + break; + } + } + } + } +} diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/dataflow/NegativelyNamedBooleanVariableInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/dataflow/NegativelyNamedBooleanVariableInspection.java new file mode 100644 index 000000000000..06cdfff5c0ab --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/dataflow/NegativelyNamedBooleanVariableInspection.java @@ -0,0 +1,35 @@ +/* + * Copyright 2000-2013 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.siyeh.ig.dataflow; + +import com.intellij.psi.PsiVariable; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.fixes.InvertBooleanFix; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class NegativelyNamedBooleanVariableInspection extends NegativelyNamedBooleanVariableInspectionBase { + + @Nullable + @Override + protected InspectionGadgetsFix buildFix(Object... infos) { + final PsiVariable variable = (PsiVariable)infos[0]; + return new InvertBooleanFix(InspectionGadgetsBundle.message("invert.quickfix", variable.getName())); + } +} diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/NegativelyNamedBooleanVariable.html b/plugins/InspectionGadgets/src/inspectionDescriptions/NegativelyNamedBooleanVariable.html new file mode 100644 index 000000000000..5f3ded7aaebe --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/NegativelyNamedBooleanVariable.html @@ -0,0 +1,9 @@ + + +Reports negatively named variables, for example 'disabled', 'hidden', 'isNotChanged'. +It is usually more clear to invert the boolean value and remove the negation from the name. + +

+New in 13 + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/dataflow/NegativelyNamedBooleanVariableInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/dataflow/NegativelyNamedBooleanVariableInspectionTest.java new file mode 100644 index 000000000000..d8ff32868bf3 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/dataflow/NegativelyNamedBooleanVariableInspectionTest.java @@ -0,0 +1,38 @@ +/* + * Copyright 2000-2013 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.siyeh.ig.dataflow; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; + +/** + * @author Bas Leijdekkers + */ +public class NegativelyNamedBooleanVariableInspectionTest extends LightInspectionTestCase { + + public void testTree() { + doTest("class X {" + + " boolean /*Boolean variable 'hidden' is negatively named*/hidden/**/ = false;" + + " boolean /*Boolean variable 'disabled' is negatively named*/disabled/**/ = false;" + + " boolean /*Boolean variable 'isNotChanged' is negatively named*/isNotChanged/**/ = false;" + + "}"); + } + + @Override + protected InspectionProfileEntry getInspection() { + return new NegativelyNamedBooleanVariableInspection(); + } +}