Skip to content

Commit

Permalink
Don’t warn CatchAndPrintStackTrace on printStackTrace overloads.
Browse files Browse the repository at this point in the history
If a specific `PrintWriter` or `PrintStream` is being passed in as an argument, chances are that using a specific output stream for the stack trace is intended.

PiperOrigin-RevId: 542238818
  • Loading branch information
java-team-github-bot authored and Error Prone Team committed Jun 21, 2023
1 parent 9dbf460 commit 8a5f35f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public class CatchAndPrintStackTrace extends BugChecker implements CatchTreeMatc
private static final Matcher<BlockTree> MATCHER =
matchSingleStatementBlock(
expressionStatement(
instanceMethod().onDescendantOf("java.lang.Throwable").named("printStackTrace")));
instanceMethod()
.onDescendantOf("java.lang.Throwable")
.named("printStackTrace")
.withNoParameters()));

@Override
public Description matchCatch(CatchTree tree, VisitorState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ public void positive() {
" // BUG: Diagnostic contains:",
" t.printStackTrace();",
" }",
" try {",
" System.err.println();",
" } catch (Throwable t) {",
" // BUG: Diagnostic contains:",
" t.printStackTrace(System.err);",
" }",
" }",
"}")
.doTest();
Expand All @@ -68,6 +62,11 @@ public void negative() {
" t.printStackTrace();",
" t.printStackTrace();",
" }",
" try {",
" System.err.println();",
" } catch (Throwable t) {",
" t.printStackTrace(System.err);",
" }",
" }",
"}")
.doTest();
Expand Down

0 comments on commit 8a5f35f

Please sign in to comment.