Back to Error Prone

SuppressWarningsWithoutExplanation

docs/bugpattern/SuppressWarningsWithoutExplanation.md

2.49.0637 B
Original Source

@SuppressWarnings should have an accompanying comment to explain why the javac warning is safe to ignore.

Rather than just suppressing the warning:

java
@SuppressWarnings("unchecked")
public ImmutableList<Object> performScaryCast(ImmutableList<String> strings) {
  return (ImmutableList<Object>) (ImmutableList<?>) strings;
}

Provide a concise explanation for why it is safe:

java
@SuppressWarnings("unchecked") // Safe covariant cast, given ImmutableList cannot be added to.
public ImmutableList<Object> performScaryCast(ImmutableList<String> strings) {
  return (ImmutableList<Object>) (ImmutableList<?>) strings;
}