IG: new "Overly long lambda expression" inspection

This commit is contained in:
Bas Leijdekkers
2016-09-29 16:50:38 +02:00
parent 93ec1f75f7
commit b16ae96559
5 changed files with 139 additions and 1 deletions
@@ -1631,6 +1631,9 @@
key="non.comment.source.statements.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.method.metrics" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.methodmetrics.NonCommentSourceStatementsInspection"/>
<localInspection groupPath="Java" language="JAVA" shortName="OverlyLongLambda" bundle="com.siyeh.InspectionGadgetsBundle"
key="overly.long.lambda.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.method.metrics"
enabledByDefault="false" level="WARNING" implementationClass="com.siyeh.ig.methodmetrics.OverlyLongLambdaInspection"/>
<localInspection groupPath="Java" language="JAVA" suppressId="MethodWithTooManyParameters" shortName="ParametersPerMethod" bundle="com.siyeh.InspectionGadgetsBundle"
key="parameters.per.method.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.method.metrics"
enabledByDefault="false" level="WARNING"
@@ -2197,4 +2197,6 @@ array.creation.without.new.keyword.family.quickfix=Add 'new' expression
malformed.set.up.tear.down.display.name=Malformed 'setUp()' or 'tearDown()'
malformed.set.up.tear.down.problem.descriptor='#ref()' has incorrect signature #loc
method.missing.return.statement.display.name=Method contains logic but is missing a 'return' statement
method.missing.return.statement.problem.descriptor=Method <code>#ref</code> contains logic but is missing a 'return' statement
method.missing.return.statement.problem.descriptor=Method <code>#ref</code> contains logic but is missing a 'return' statement
overly.long.lambda.display.name=Overly long lambda expression
overly.long.lambda.problem.descriptor=Lambda expression is too long (# Non-comment source statements = {0}) #loc
@@ -0,0 +1,80 @@
/*
* Copyright 2000-2016 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.methodmetrics;
import com.intellij.psi.*;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
public class OverlyLongLambdaInspection extends MethodMetricInspection {
private static final int DEFAULT_LIMIT = 3;
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message("overly.long.lambda.display.name");
}
@Override
protected int getDefaultLimit() {
return DEFAULT_LIMIT;
}
@Override
protected String getConfigurationLabel() {
return InspectionGadgetsBundle.message("non.comment.source.statements.limit.option");
}
@Override
@NotNull
public String buildErrorString(Object... infos) {
final Integer statementCount = (Integer)infos[0];
return InspectionGadgetsBundle.message("overly.long.lambda.problem.descriptor", statementCount);
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new OverlyLongLambdaVisitor();
}
private class OverlyLongLambdaVisitor extends BaseInspectionVisitor {
@Override
public void visitLambdaExpression(PsiLambdaExpression expression) {
final PsiElement body = expression.getBody();
if (!(body instanceof PsiCodeBlock)) {
return;
}
final PsiCodeBlock block = (PsiCodeBlock)body;
final PsiJavaToken brace = block.getLBrace();
if (brace == null) {
return;
}
final NCSSVisitor visitor = new NCSSVisitor();
block.accept(visitor);
final int count = visitor.getStatementCount();
if (count <= getLimit()) {
return;
}
registerErrorAtOffset(expression, 0, body.getStartOffsetInParent() + 1, Integer.valueOf(count));
}
}
}
@@ -0,0 +1,12 @@
<html>
<body>
Reports lambda expressions that are too long.
Lambda expressions that are too long
may be confusing, and it is often better to extract the statements into their own method.
<!-- tooltip end -->
<p>
Use the field below to specify the maximum number of non-comment source statements a lambda expression is allowed to have.
<p>
<small>New in 2016.3</small>
</body>
</html>
@@ -0,0 +1,41 @@
/*
* Copyright 2000-2016 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.methodmetrics;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers
*/
public class OverlyLongLambdaInspectionTest extends LightInspectionTestCase {
public void testSimple() {
doStatementTest("Runnable r = /*Lambda is too long (# Non-comment source statements = 4)*/() -> {/**/" +
" System.out.println();" +
" System.out.println();" +
" System.out.println();" +
" System.out.println();" +
"};");
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new OverlyLongLambdaInspection();
}
}