From 72540b1b2fe91c330e14f8e30cf2908f46545cea Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Thu, 20 Jul 2017 15:14:09 +0300 Subject: [PATCH] WEB-27035 Redundant default attribute value assignment. --- .../daemon/XmlInspectionsTest.java | 6 +++ .../XmlDefaultAttributeValueInspection.java | 50 ++++++++++--------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/xml/tests/src/com/intellij/codeInsight/daemon/XmlInspectionsTest.java b/xml/tests/src/com/intellij/codeInsight/daemon/XmlInspectionsTest.java index 726715aa85bb..8a9729f3c430 100644 --- a/xml/tests/src/com/intellij/codeInsight/daemon/XmlInspectionsTest.java +++ b/xml/tests/src/com/intellij/codeInsight/daemon/XmlInspectionsTest.java @@ -57,4 +57,10 @@ public class XmlInspectionsTest extends LightPlatformCodeInsightFixtureTestCase "\n"); myFixture.checkHighlighting(); } + + public void testDefaultAttributeInHtml() { + myFixture.enableInspections(new XmlDefaultAttributeValueInspection()); + myFixture.configureByText(HtmlFileType.INSTANCE, ""); + myFixture.checkHighlighting(); + } } diff --git a/xml/xml-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/XmlDefaultAttributeValueInspection.java b/xml/xml-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/XmlDefaultAttributeValueInspection.java index 1303d84d410a..1834edc70e0c 100644 --- a/xml/xml-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/XmlDefaultAttributeValueInspection.java +++ b/xml/xml-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/XmlDefaultAttributeValueInspection.java @@ -21,6 +21,7 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElementVisitor; import com.intellij.psi.XmlElementVisitor; +import com.intellij.psi.html.HtmlTag; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.xml.XmlAttribute; import com.intellij.psi.xml.XmlAttributeValue; @@ -40,30 +41,33 @@ public class XmlDefaultAttributeValueInspection extends XmlSuppressableInspectio @Override public void visitXmlAttributeValue(XmlAttributeValue value) { PsiElement parent = value.getParent(); - if (parent instanceof XmlAttribute) { - XmlAttributeDescriptor descriptor = ((XmlAttribute)parent).getDescriptor(); - if (descriptor != null) { - String defaultValue = descriptor.getDefaultValue(); - if (defaultValue != null && defaultValue.equals(value.getValue())) { - holder.registerProblem(value, "Redundant default attribute value assignment", ProblemHighlightType.LIKE_UNUSED_SYMBOL, - new LocalQuickFix() { - @Nls - @NotNull - @Override - public String getFamilyName() { - return XmlErrorMessages.message("remove.attribute.quickfix.family"); - } + if (!(parent instanceof XmlAttribute)) { + return; + } + if (parent.getParent() instanceof HtmlTag && "input".equals(((HtmlTag)parent.getParent()).getName())) return; + XmlAttributeDescriptor descriptor = ((XmlAttribute)parent).getDescriptor(); + if (descriptor == null) { + return; + } + String defaultValue = descriptor.getDefaultValue(); + if (defaultValue != null && defaultValue.equals(value.getValue())) { + holder.registerProblem(value, "Redundant default attribute value assignment", ProblemHighlightType.LIKE_UNUSED_SYMBOL, + new LocalQuickFix() { + @Nls + @NotNull + @Override + public String getFamilyName() { + return XmlErrorMessages.message("remove.attribute.quickfix.family"); + } - @Override - public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { - XmlAttribute attribute = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), XmlAttribute.class); - if (attribute != null) { - attribute.delete(); - } - } - }); - } - } + @Override + public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { + XmlAttribute attribute = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), XmlAttribute.class); + if (attribute != null) { + attribute.delete(); + } + } + }); } } };