Skip to content

Commit

Permalink
ignore any constructor for exceptions in unused code check #426
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Feb 9, 2024
1 parent cd6ee80 commit d7010b6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ private void checkUnused(StaticAnalysis staticAnalysis, CtNamedElement ctElement
return;
}

// ignore exception constructors and params in those constructors
CtConstructor<?> parentConstructor = ctElement.getParent(CtConstructor.class);
if (parentConstructor == null && ctElement instanceof CtConstructor<?> ctConstructor) {
parentConstructor = ctConstructor;
}

if (parentConstructor != null && SpoonUtil.isSubtypeOf(parentConstructor.getType(), java.lang.Throwable.class)) {
return;
}

Predicate<CtElement> shouldVisit = element -> true;
if (ctElement instanceof CtMethod<?> method) {
// Methods are also unused if they are only referenced by themselves, i.e. they are called recursively
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,4 +538,39 @@ public static void main(String[] args) {

assertEquals(0, problems.size());
}

@Test
void testConventionExceptionConstructor() throws LinterException, IOException {
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceStrings(
JavaVersion.JAVA_17,
Map.ofEntries(
Map.entry(
"Main",
"""
public class Main {
public static void main(String[] args) {
throw new MyException("abc123", 123);
}
}
"""
),
Map.entry(
"MyException",
"""
public class MyException extends RuntimeException {
public MyException() {}
public MyException(String message) { super(message); }
public MyException(String message, Throwable cause) { super(message, cause); }
public MyException(Throwable cause) { super(cause); }
public MyException(String message, int number) { super(message + number); } // this is used
}
"""
)
)
), PROBLEM_TYPES);

problems.assertExhausted();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -667,4 +667,39 @@ void b(String parameterName) {}

problems.assertExhausted();
}

@Test
void testConventionExceptionConstructor() throws LinterException, IOException {
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceStrings(
JavaVersion.JAVA_17,
Map.ofEntries(
Map.entry(
"Main",
"""
public class Main {
public static void main(String[] args) {
throw new MyException("abc123", 123);
}
}
"""
),
Map.entry(
"MyException",
"""
public class MyException extends RuntimeException {
public MyException() {}
public MyException(String message) { super(message); }
public MyException(String message, Throwable cause) { super(message, cause); }
public MyException(Throwable cause) { super(cause); }
public MyException(String message, int number) { super(message + number); } // this is used
}
"""
)
)
), PROBLEM_TYPES);

problems.assertExhausted();
}
}

0 comments on commit d7010b6

Please sign in to comment.