Package com.google.errorprone.bugpatterns
package com.google.errorprone.bugpatterns
Checks added to the java compiler which detect common bug patterns.
-
ClassDescriptionCheck for usage of
Set<T>
orMap<T, E>
.An abstract class that detects use of the unsafe APIs.AbstractMockChecker<T extends Annotation>Helper for enforcing Annotations that disallow mocking.A policy for determining what classes should not be mocked.An explanation of what type should not be mocked, and the reason why.AbstractMockChecker.TypeExtractor<T extends com.sun.source.tree.Tree>An extension ofMatcher
to return, not just a boolean `matches`, but also extract some type information about the Tree of interest.An abstract check for resources that must be closed; used byStreamResourceLeak
andMustBeClosedChecker
.Error Prone's fix application logic doesn't like it when a fix suggests multiple identical insertions at the same position.Finds calls to regex-accepting methods with literal strings.Abstract implementation of a BugPattern that detects the use of reference equality to compare classes with value semantics.An abstract base class to match API usages in which the return value is not used.An abstract matcher for implicit and explicit calls toObject.toString()
, for use on types that do not have a human-readabletoString()
implementation.Helper for refactoring from if-else chains to switches.ABugChecker
; see the associatedBugPattern
annotation for details.Bugpattern to find conditions which are checked more than once.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Detects occurrences of pairs of parameters being passed straight through toString.format(java.lang.String, java.lang.Object...)
from a method not annotated withFormatMethod
.ABugChecker
; see the associatedBugPattern
annotation for details.Checks annotation positioning, and orphaned Javadocs.ABugChecker
; see the associatedBugPattern
annotation for details.Warns that users should not have an array as a key to a Set or MapChecks when Arrays.fill(Object[], Object) is called with object types that are guaranteed to result in an ArrayStoreException.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Utility methods for refactoring try-fail pattern to assertThrows, which is preferred.A BugPattern; see the summaryChecks thatAsyncCallable
implementations do not directlyreturn null
.Checks thatAsyncFunction
implementations do not directlyreturn null
.A BugPattern; see the summary.See summary for details.See summary for details.Checks the toString(), hashCode() and equals() methods are final in AutoValue classes.Flags mutable collections in AutoValue.MatchesAutoValue_
uses outside the containing file.ABugChecker
; see the associatedBugPattern
annotation for details.Checker that ensures implementations ofAnnotation
override equals and hashCode.Matches instanceof checks where the expression is a subtype of the checked type.ABugChecker
that detects use of the unsafe JNDI API system.ABugChecker
that detects use of the unsafe JNDI API system.ABugChecker
that detects use of the unsafeSerializable
API.A BugChecker; see the associated BugPattern for details.Matches use ofBigDecimal#equals
, which compares scale as well (which is not likely to be intended).Matches usages ofnew BigDecimal(double)
which lose precision.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.A base class for implementing bug checkers.See thesummary
.Checks when ByteBuffer.array() is used without calling .arrayOffset() to know the offset of the array, or when the buffer wasn't initialized using ByteBuffer.wrap() or ByteBuffer.allocate().ABugChecker
; see the associatedBugPattern
annotation for details.Analyzes trees for references to their enclosing instance.Stores the result of a can-be-static query.Bug pattern to recognize attempts to mock final types.A BugPattern; see the summaryABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Flags code which catchesRuntimeException
s under the guise of catchingException
.Identifies calls toassertThat
and similar methods inside the implementation of aSubject
assertion method.Checks, if two constructors in a class both acceptFoo foo
and one calls the other, that the caller passesfoo
as a parameter.Checks for use of Character.getNumericValue and UCharacter.getNumericValueFlags checked exceptions which are claimed to be thrown, but are not.Checks for the same variable being checked against null twice in a method.See the summary.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.CommonAutoCloseable
decorator types that wrap aroundAutoCloseable
resources, which are always closed when the enclosing decorators are closed.ABugChecker
; see the associatedBugPattern
annotation for details.Suggests comparing the result ofcompareTo
to only0
.Check for expressions containingthis != null
orthis == null
ABugChecker
; see the associatedBugPattern
annotation for details.A Bugpattern; see the accompanying Markdown documentation.Flags ambiguous creations of objects inMap.computeIfAbsent(K, java.util.function.Function<? super K, ? extends V>)
.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Flags variables initialized withPattern.compile(String)
calls that could be constants.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.A checker that suggests deduplicating literals with existing constant variables.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Java classes shouldn't use default package.Matches the behaviour of the javac dep-ann Xlint warning.ABugChecker
; see the associatedBugPattern
annotation for details.Discourages using multiple names to refer to the same type within a file (e.g.A bugpattern; see the description.ABugChecker
; see the associatedBugPattern
annotation for details.ErrorProne checker to generate warning when method expecting distinct varargs is invoked with same variable argument.ABugChecker
; see the associatedBugPattern
annotation for details.If a method always throws an exception, consider annotating it with@DoNotCall
to prevent calls at compile-time instead failing at runtime.ABugChecker
; see the associatedBugPattern
annotation for details.Suggests not mocking AutoValue classes.Points out if a Mockito or EasyMock mock is mocking an object that would be better off being tested using an alternative instance.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Flag DateFormats which use the same field more than once.Flags duplicate keys used in ImmutableMap construction.ABugChecker
; see the associatedBugPattern
annotation for details.This checker finds and fixes empty statements after an if, with no else part.ABugChecker
; see the associatedBugPattern
annotation for details.Discourages the use ofEnum.ordinal()
and other ways to access enum values by index.Discourages the use ofObject.getClass()
when implementingObject.equals(Object)
for non-final classes.Classes that overrideObject.equals(java.lang.Object)
should also overrideObject.hashCode()
.ABugChecker
; see the associatedBugPattern
annotation for details.Checks forequals
implementations making unsafe casts.Discourages implementingequals
usinghashCode
.Checks forequals
implementations comparing non-corresponding fields.A BugPattern; see the summary.ErrorProne checker to generate warning wheneverThreadPoolExecutor
is constructed with differentcorePoolSize
andmaximumPoolSize
using an unboundedworkQueue
ABugChecker
; see the associatedBugPattern
annotation for details.Makes sure that you are not extending a class that has @AutoValue as an annotation.ABugChecker
; see the associatedBugPattern
annotation for details.Flags fields which can be replaced with local variables.Finds fields which can be safely made static.ABugChecker
; see the associatedBugPattern
annotation for details.Matches the behaviour of javac's finally Xlint warning.ABugChecker
; see the associatedBugPattern
annotation for details.Detects usages ofFloat,DoubleSubject.isWithin(TOLERANCE).of(EXPECTED)
where there are no other floating point values other thanEXPECTED
with satisfy the assertion, butTOLERANCE
is not zero.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Verifies that methods markedForOverride
are only called from the defining class.ABugChecker
; see the associatedBugPattern
annotation for details.See BugPattern annotation.Checks for calls to Guava'sFutures.getChecked
method that will always fail because they pass an incompatible exception type.See summary for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Flags a few ways in which member names may violate the style guide.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Flags ignored return values from pure getters.Common utility functions for immutable collections.Refactoring to suggest Immutable types for member collection that are not mutated.Refactoring to suggest usingprivate static final
ImmutableSet
overImmutableList
when using only contains, containsAll and isEmpty.Migrates Truth subjects from a manual "test and fail" approach to one usingSubject.check(...)
.Matches comparison of proto fields tonull
.ABugChecker
; see the associatedBugPattern
annotation for details.Checker for variables under the same scope that only differ in capitalization.Looks for hashCode implementations which are inconsistent with equals.Bugpattern for incorrect overloads of main.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Bugpattern to encourage initializing effectively final variables inline with their declaration, if possible.A BugPattern; see the summary.ABugChecker
; see the associatedBugPattern
annotation for details.Checks that InputStreams should override int read(byte[], int, int);Bugpattern to detect interfaces used only to store static fields/methods.Checks for cases where anInterruptedException
is caught as part of a catch block catching a supertype, and not specially handled.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Validates ZoneId.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Detects floating-point assertEquals() calls that will not work in JUnit 4.A bugpattern; see the associated summary.@BeforeClass
or@AfterClass
should be applied to static methods.Checks for the existence of a JUnit3 style setUp() method in a JUnit4 test class or methods annotated with a non-JUnit4 @Before annotation.Checks for the existence of a JUnit3 style tearDown() method in a JUnit4 test class or methods annotated with a non-JUnit4 @After annotation.Finds tests that won't run due to the enclosing runner.Points out if an object is tested for reference equality to itself using JUnit library.Checks if the methods specified injunitparams.Parameters
annotation to provide parameters exists.A BugPattern; see the summary.A BugPattern; see the summary.Points out if #valueOf() is called on a Protocol Buffer Enum.Flags calls totoString
on lite protos.Suggests that calls toLock.lock
must be immediately followed by atry-finally
that callsLock.unlock
.Detects locks on boxed primitives.Bug checker to detect the usage of lock on the class other than the enclosing class of the code block.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Matcher for along
literal with a lower-case ell for a suffix (e.g.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Checker to prevent usages of comparison methods where both the operands undergo lossy widening.Check for calls to Math'sMath.round(float)
with an integer or long parameter.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.See the summary.See the summary.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Matches test helpers which require a terminating method to be called.Base class for checks which find common errors in date format patterns.Ban use of D (day-of-year) in a date format pattern that also contains M (month-of-year).Ban use of YYYY in a SimpleDateFormat pattern, unless it is being used for a week date.ABugChecker
; see the associatedBugPattern
annotation for details.Checks for calls toDescriptor#findFieldByNumber
with field numbers from a different proto.Flags methods which return mutable collections from some code paths, but immutable ones from others.A BugPattern; see the summary.ABugChecker
; see the associatedBugPattern
annotation for details.A BugPattern; see the summary.Matches creation of new collections/proto builders which are modified but never used.Identify the backing collection source of a stream and reports if the source is mutated during the stream operations.A BugPattern; see the summary.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Checks if a constructor or method annotated withMustBeClosed
is called within the resource variable initializer of a try-with-resources statement.ABugChecker
; see the associatedBugPattern
annotation for details.Check for public static final declaration of Arrays.Warns on classes or methods being named similarly to contextual keywords, or invoking such methods.A BugPattern; see the summary.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Checks methods annotated with@NoAllocation
to ensure they really do not allocate.Flags instances of non-API types from being accepted or returned in APIs.Detects non-atomic updates to volatile variables.Types shouldn't be statically by their non-canonical name.Members shouldn't be statically imported by their non-canonical name.Flags types being referred to by their non-canonical name.Enforce that @CompileTimeConstant parameters are final or effectively final.A BugPattern; see the summary.Bug checker for equals methods that don't actually override equals.ABugChecker
; see the associatedBugPattern
annotation for details.A bugpattern; see the summary.ABugChecker
; see the associatedBugPattern
annotation for details.Flags passing literal null toOptional
-accepting APIs.ABugChecker
; see the associatedBugPattern
annotation for details.Check for usage ofObjects.equal
on primitive types.Check for calls to Objects'Objects.hashCode(java.lang.Object)
with a primitive parameter.Warns against calling toString() on Objects which don't have toString() method overridden and won't produce meaningful output.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.MatchesOptional#map
mapping to anotherOptional
.ReplacesOptional.map
withOptional.ifPresent
if the value is unused.Checks ifOptional#of
is chained with a redundant method.ABugChecker
; see the associatedBugPattern
annotation for details.Check for the a11y antipattern of setting CSS outline attributes to none or 0.Matches the behaviour of javac's overrides Xlint warning.Warns against overriding toString() in a Throwable class and suggests getMessage()ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Flags uses of parameters in non-parameterized tests.A BugPattern; see the summary.Checks that Precondition.checkNotNull is not invoked with same arg twice.Tightens types which refer to an Iterable, Map, Multimap, etc.Discourages inadvertently using reference equality on boxed primitives in AtomicReference.Check for disallowed access to private_do_not_access_or_else proto fields.Flags protected members in final classes.Highlights cases where a proto's build method has its return value ignored.Points out if #ordinal() is called on a Protocol Buffer Enum.Checks thatProtoTruth
'signoringFields
is passed field numbers from the correct proto.Checks if public APIs named "stream" returns a type whose name ends with Stream.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Removes overrides which purely pass through to the method in the super class.A BugPattern; see the summary.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Utility methods to find replacement variables with similar names.Check for non-allowlisted callers to RestrictedApiChecker.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Flags methods with collection return types which returnnull
in some cases but don't annotate the method as @Nullable.A checker which produces an error when a return value is accidentally discarded.ABugChecker
; see the summary.ABugChecker
; see the associatedBugPattern
for details.Looks for types being shadowed by other types in a way that may be confusing.ABugChecker
; see the associatedBugPattern
annotation for details.ABugPattern
; see the summary.TODO(eaftan): Consider cases where the parent is not a statement or there is no parent?Points out if an object is compared to itself.List of banned methods forBanSerializableRead
.A BugPattern; see the summary.Replaces the operators when visiting the binary nodesChecks that variable argument methods have even number of arguments.Finds instances where one usesCollection#size() >= 0
orT[].length > 0
.Checks for statement switches that can be expressed as an equivalent expression switch.Checks for static fields being assigned within constructors.Checks for static fields being assigned withThrowable
.Logic for inspecting static imports used byNonCanonicalStaticImport
,NonCanonicalStaticMemberImport
, andUnnecessaryStaticImport
.Information about a static import.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Discouragestream::iterator
to createIterable
s.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
that flags calls toString.toLowerCase()
andString.toUpperCase()
, as these methods implicitly rely on the environment's default locale.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Helper for strongly typing fields.Builder forStronglyType
Flags fields which would be better expressed as ByteStrings rather than primitive byte arrays.Check for calls to String'sfoo.substring(0)
.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Find uses of SuppressWarnings with "deprecated".Finds occurrences of@SuppressWarnings
where there is definitely no explanation for why it is safe.ABugChecker
; see the associatedBugPattern
annotation for details.Flagsjavax.lang.model.element.Element#toString
usage inBugChecker
s.ABugChecker
; see the associatedBugPattern
annotation for details.Check for calls toSystem.exit()
outside of a main method.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Flags uses of parameters in non-parameterized tests.Flags uses of@Theory
(and others) in non-Theories
-run tests.ABugChecker
; see the associatedBugPattern
annotation for details.A BugPattern; see the summary.A BugPattern; see the summary.ABugChecker
; see the associatedBugPattern
annotation for details.Bugpattern to discourage throwing base exception classes.Suggests to remove the unchecked throws clause.ABugChecker
; see the associatedBugPattern
annotation for details.ToString should not return null.ABugChecker
; see the associatedBugPattern
annotation for details.Warns against use of bothstatic
andtransient
modifiers on field declarations.Flagscom.sun.source.tree.Tree#toString
usage inBugChecker
s.Detects usages of Truth assertions with the expected and actual values reversed.Points out if Truth Library assert is called on a constant.ABugChecker
; see the associatedBugPattern
annotation for details.Flags ambiguous usages ofMap#getOrDefault
withinTruth#assertThat
.ABugChecker
; see the associatedBugPattern
annotation for details.A bug checker for the following code pattern:ABugChecker
; see the associatedBugPattern
annotation for details.Methods to answer the question: are these two types "compatible" with each other, in the context of an equality check?Flags com.sun.tools.javac.code.Type#equals usage.Warns when a type parameter shadows another type name in scope.Enforces type parameters match the google style guide.An enum that classifies a String name into different types, based on the Google Java Style Guide's rules for Type Parameters.ABugChecker
; see the associatedBugPattern
annotation for details.Enumerates types which have poorly-defined behaviour for equals.Flagsjavax.lang.model.type.TypeMirror#toString
usage inBugChecker
s.Flags types which do not have well-defined equals behavior.Bans, without the possibility of suppression, the use of direction-changing Unicode escapes.Replaces printable ASCII unicode escapes with the literal version.Bans using non-ASCII Unicode characters outside string literals and comments.Flags uses of fully qualified names which are not ambiguous if imported.Suggests restricting the visibility of methods which should only be called by a framework.ABugChecker
; see the associatedBugPattern
annotation for details.Discourage manual initialization or assignment to fields annotated with framework annotations.A BugPattern; see the summary.Finds and fixes unnecessarily boxed return expressions.Finds and fixes unnecessarily boxed variables.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Removesfinal
from non-field variables.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Matchers.methodInvocation(Matcher)
is not exactly deprecated, but it is legacy, and in particular is not needed when the argument is a MethodMatcher, since MethodMatcher already does the unwrapping that methodInvocation does.Matches unnecessary uses of method references.A refactoring to replace Optional.get() with lambda arg in expressions passed as arg to member functions of Optionals.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.See the summary.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Flags unsafe usages of theLocale
constructor and class methods.Checks unsafe instance creation via reflection.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Bugpattern for catch blocks which catch an exception but throw another one without wrapping the original.A BugPattern; see the summary.Bugpattern to detect unused declarations.Bugpattern to detect unused nested classes.A BugPattern; see the summary.Bugpattern to detect unused declarations.Points out on creation of Set and HashMap of type java.net.URL.ABugChecker
; see the associatedBugPattern
annotation for details.ABugChecker
; see the associatedBugPattern
annotation for details.Check for variables and types with the same nameConverts some local variables to usevar
.ABugChecker
; see the associatedBugPattern
annotation for details.A BugPattern; see the summary.ABugChecker
; see the associatedBugPattern
annotation for details.MethodMatchers.MethodClassMatcher.withSignature(String)
is discouraged: most usages should use .named and/or .withParameters instead.Matches always-default expressions in oneof switches.ABugChecker
; see the associatedBugPattern
annotation for details.See the summary.