Skip to content

Commit

Permalink
Merge pull request #470 from Luro02/main
Browse files Browse the repository at this point in the history
Implement some things
  • Loading branch information
Luro02 authored Apr 2, 2024
2 parents 3a53c76 + 114fd18 commit 466bee3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.code.CtOperatorAssignment;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtVariableAccess;
Expand Down Expand Up @@ -161,6 +162,11 @@ private void checkArraysFill(CtFor ctFor) {
return;
}

// ignore new array or new class assignments
if (ctAssignment.getAssignment() instanceof CtNewClass<?> || ctAssignment.getAssignment() instanceof CtNewArray<?>) {
return;
}

CtExpression<?> rhs = ctAssignment.getAssignment();
if (!SpoonUtil.isImmutable(rhs.getType())) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ private String buildFormattedString(Iterable<? extends CtExpression<?>> ctExpres
} else {
formatString.append(value);
}

// if the string ends with a %, the concatenation is likely used to build a format string
if (value.endsWith("%")) {
return null;
}
} else if (ctTypeInformation.isPrimitive() && !ctTypeInformation.isArray()) {
// inline literals:
formatString.append(ctLiteral.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

@ExecutableCheck(reportedProblems = {ProblemType.REDUNDANT_VARIABLE})
public class RedundantVariable extends IntegratedCheck {
private static final int MAX_EXPRESSION_SIZE = 40;

/**
* Checks if the given statement does not influence the variable returned by the return statement.
*
Expand All @@ -32,7 +34,7 @@ private boolean isAllowedStatement(CtStatement ctStatement) {
}

private boolean isComplexExpression(CtExpression<?> ctExpression) {
return ctExpression instanceof CtSwitchExpression<?,?>;
return ctExpression instanceof CtSwitchExpression<?,?> || ctExpression.toString().length() > MAX_EXPRESSION_SIZE;
}

private void checkVariableRead(CtStatement ctStatement, CtVariableRead<?> ctVariableRead) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private <T> void checkCtMethod(CtMethod<T> ctMethod) {
return;
}

if (prefix.equals("set") && isInvalidSetterReturnType(ctMethod)) {
if (prefix.equals("set") && isInvalidSetterReturnType(ctMethod) && SpoonUtil.getEffectiveStatements(ctMethod.getBody()).size() <= 3) {
// it is expected that a setter returns nothing (void)
this.reportProblem(
"linguistic-naming-setter",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public void process(CtClass<?> ctClass) {
}

// check if the class can be an interface:
if (fields.isEmpty()) {
if (fields.isEmpty() && ctClass.getSuperclass() == null) {
addLocalProblem(
ctClass,
new LocalizedMessage("should-be-interface"),
ProblemType.SHOULD_BE_INTERFACE
);
// check if the class has only fields (data class)
} else if (methods.isEmpty()) {
} else if (methods.isEmpty() && ctClass.getSuperclass() == null) {
String methodName = IdentifierNameUtils.toLowerCamelCase(ctClass.getSimpleName());
addLocalProblem(
ctClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,26 @@ public String toString() {

problems.assertExhausted();
}

@Test
void testBuildFormatString() throws LinterException, IOException {
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceString(
JavaVersion.JAVA_17,
"Field",
"""
public class Field {
private String left;
private int number;
private String right;
@Override
public String toString() {
return String.format("output: " + "%" + this.number + "d", this.number);
}
}
"""
), PROBLEM_TYPES);

problems.assertExhausted();
}
}

0 comments on commit 466bee3

Please sign in to comment.