mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-266665 - added inspection that reports redundant modifiers in records
GitOrigin-RevId: 6774066ca0e4663228aca9e2921184ea74c2938a
This commit is contained in:
committed by
intellij-monorepo-bot
parent
aa13dfdc40
commit
bd806744fb
+3
-1
@@ -776,7 +776,8 @@ empty.try.block.display.name=Empty 'try' block
|
||||
field.has.setter.but.no.getter.display.name=Field has setter but no getter
|
||||
three.negations.per.method.display.name=Method with more than three negations
|
||||
conditional.expression.display.name=Conditional expression
|
||||
unnecessary.enum.modifier.display.name=Unnecessary enum modifier
|
||||
unnecessary.enum.modifier.display.name=Unnecessary 'enum' modifier
|
||||
unnecessary.record.modifier.display.name=Unnecessary 'record' modifier
|
||||
string.equals.empty.string.display.name='String.equals()' can be replaced with 'String.isEmpty()'
|
||||
synchronize.on.lock.display.name=Synchronization on a Lock object
|
||||
synchronized.on.literal.object.name=Synchronization on an object initialized with a literal
|
||||
@@ -1290,6 +1291,7 @@ too.broad.scope.narrow.quickfix=Move declaration of ''{0}'' closer to usages
|
||||
press.escape.to.remove.highlighting.message=Press Escape to remove the highlighting
|
||||
unnecessary.enum.modifier.problem.descriptor=Modifier <code>#ref</code> is redundant for enum constructors #loc
|
||||
unnecessary.enum.modifier.problem.descriptor1=Modifier <code>#ref</code> is redundant for inner enums #loc
|
||||
unnecessary.redundant.modifier.problem.descriptor=Modifier <code>#ref</code> is redundant for records
|
||||
literal.as.arg.to.string.equals.problem.descriptor=Literal #ref is argument of ''{0}()'', instead of its qualifier #loc
|
||||
literal.as.arg.to.string.equals.flip.quickfix=Flip ''{0}()''
|
||||
c.style.array.declaration.replace.quickfix=Replace with Java-style array declaration
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.siyeh.ig.style;
|
||||
|
||||
import com.intellij.codeInspection.CleanupLocalInspectionTool;
|
||||
import com.intellij.codeInspection.ProblemHighlightType;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.psi.PsiModifierList;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import com.siyeh.ig.fixes.RemoveModifierFix;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class UnnecessaryRecordModifierInspection extends BaseInspection implements CleanupLocalInspectionTool {
|
||||
|
||||
@Override
|
||||
protected @NotNull String buildErrorString(Object... infos) {
|
||||
return InspectionGadgetsBundle.message("unnecessary.redundant.modifier.problem.descriptor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseInspectionVisitor buildVisitor() {
|
||||
return new UnnecessaryRecordModifierVisitor();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable InspectionGadgetsFix buildFix(Object... infos) {
|
||||
return new RemoveModifierFix((String)infos[0]);
|
||||
}
|
||||
|
||||
private static class UnnecessaryRecordModifierVisitor extends BaseInspectionVisitor {
|
||||
@Override
|
||||
public void visitClass(PsiClass aClass) {
|
||||
if (!aClass.isRecord()) return;
|
||||
PsiModifierList modifiers = aClass.getModifierList();
|
||||
if (modifiers == null) return;
|
||||
for (PsiElement modifier : modifiers.getChildren()) {
|
||||
String modifierText = modifier.getText();
|
||||
if (PsiModifier.FINAL.equals(modifierText) || !PsiUtil.isLocalClass(aClass) && PsiModifier.STATIC.equals(modifierText)) {
|
||||
registerError(modifier, ProblemHighlightType.LIKE_UNUSED_SYMBOL, modifierText);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2403,6 +2403,10 @@
|
||||
key="unnecessary.qualifier.for.this.display.name" groupBundle="messages.InspectionsBundle"
|
||||
groupKey="group.names.code.style.issues" enabledByDefault="false" level="WARNING" cleanupTool="true"
|
||||
implementationClass="com.siyeh.ig.style.UnnecessaryQualifierForThisInspection"/>
|
||||
<localInspection groupPath="Java" language="JAVA" shortName="UnnecessaryRecordModifier" bundle="messages.InspectionGadgetsBundle"
|
||||
key="unnecessary.record.modifier.display.name" groupBundle="messages.InspectionsBundle"
|
||||
groupKey="group.names.code.style.issues" enabledByDefault="true" level="WARNING" cleanupTool="true"
|
||||
implementationClass="com.siyeh.ig.style.UnnecessaryRecordModifierInspection"/>
|
||||
<localInspection groupPath="Java" language="JAVA" shortName="UnnecessarySemicolon" bundle="messages.InspectionGadgetsBundle" key="unnecessary.semicolon.display.name"
|
||||
groupBundle="messages.InspectionsBundle" groupKey="group.names.code.style.issues" enabledByDefault="true" cleanupTool="true"
|
||||
level="WARNING" implementationClass="com.siyeh.ig.style.UnnecessarySemicolonInspection"/>
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
<html>
|
||||
<body>
|
||||
Reports any redundant modifier on records.
|
||||
<p>Example:</p>
|
||||
<pre><code>
|
||||
final record R() {
|
||||
}
|
||||
|
||||
class Test {
|
||||
static record R() {
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
<!-- tooltip end -->
|
||||
<p>
|
||||
</body>
|
||||
</html>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<warning descr="Modifier 'final' is redundant for records">final</warning> record R() {
|
||||
}
|
||||
|
||||
class C {
|
||||
<warning descr="Modifier 'static' is redundant for records">static</warning> record R () {
|
||||
}
|
||||
|
||||
void test() {
|
||||
<error descr="Modifier 'static' not allowed here">static</error> <warning descr="Modifier 'final' is redundant for records">final</warning> record R () {
|
||||
}
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.siyeh.ig.style;
|
||||
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
import com.siyeh.ig.LightJavaInspectionTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class UnnecessaryRecordModifierInspectionTest extends LightJavaCodeInsightFixtureTestCase {
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return LightJavaInspectionTestCase.INSPECTION_GADGETS_TEST_DATA_PATH + "com/siyeh/igtest/style/unnecessary_record_modifier";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected LightProjectDescriptor getProjectDescriptor() {
|
||||
return JAVA_16;
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
myFixture.enableInspections(new UnnecessaryRecordModifierInspection());
|
||||
myFixture.testHighlighting(getTestName(false) + ".java");
|
||||
}
|
||||
|
||||
public void testUnnecessaryModifier() {
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user