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 Tree>An extension ofMatcherto 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 byStreamResourceLeakandMustBeClosedChecker.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 associatedBugPatternannotation for details.Bugpattern to find conditions which are checked more than once.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Checks annotation positioning, and orphaned Javadocs.ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Utility methods for refactoring try-fail pattern to assertThrows, which is preferred.A BugPattern; see the summary.A BugPattern; see the summaryChecks thatAsyncCallableimplementations do not directlyreturn null.Checks thatAsyncFunctionimplementations 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 associatedBugPatternannotation for details.Checker that ensures implementations ofAnnotationoverride equals and hashCode.Matches instanceof checks where the expression is a subtype of the checked type.ABugCheckerthat detects use of the unsafe JNDI API system.ABugCheckerthat detects use of the unsafe JNDI API system.ABugCheckerthat detects use of the unsafeSerializableAPI.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.Simplifies Boolean.TRUE/FALSE to true/false.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.Analyzes trees for references to their enclosing instance.Bug pattern to recognize attempts to mock final types.A BugPattern; see the summaryABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Flags code which catchesRuntimeExceptions under the guise of catchingException.Identifies calls toassertThatand similar methods inside the implementation of aSubjectassertion method.Checks, if two constructors in a class both acceptFoo fooand one calls the other, that the caller passesfooas 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 associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.CommonAutoCloseabledecorator types that wrap aroundAutoCloseableresources, which are always closed when the enclosing decorators are closed.ABugChecker; see the associatedBugPatternannotation for details.Suggests comparing the result ofcompareToto only0.Check for expressions containingthis != nullorthis == nullABugChecker; see the associatedBugPatternannotation for details.A Bugpattern; see the accompanying Markdown documentation.Flags ambiguous creations of objects inMap.computeIfAbsent(K, Function).ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Flags variables initialized withPattern.compile(String)calls that could be constants.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.A checker that suggests deduplicating literals with existing constant variables.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Java classes shouldn't use default package.Matches the behaviour of the javac dep-ann Xlint warning.ABugChecker; see the associatedBugPatternannotation for details.Discourages using multiple names to refer to the same type within a file (e.g. bothOuterClass.InnerClassandInnerClass).A bugpattern; see the description.ABugChecker; see the associatedBugPatternannotation for details.ErrorProne checker to generate warning when method expecting distinct varargs is invoked with same variable argument.ABugChecker; see the associatedBugPatternannotation for details.If a method always throws an exception, consider annotating it with@DoNotCallto prevent calls at compile-time instead failing at runtime.ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Flag DateFormats which use the same field more than once.Flags duplicate keys used in ImmutableMap construction.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.This checker finds and fixes empty statements after an if, with no else part.ABugChecker; see the associatedBugPatternannotation 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(Object)should also overrideObject.hashCode().ABugChecker; see the associatedBugPatternannotation for details.Checks forequalsimplementations making unsafe casts.Discourages implementingequalsusinghashCode.Checks forequalsimplementations comparing non-corresponding fields.A BugPattern; see the summary.ErrorProne checker to generate warning wheneverThreadPoolExecutoris constructed with differentcorePoolSizeandmaximumPoolSizeusing an unboundedworkQueueABugChecker; see the associatedBugPatternannotation for details.Error checker for calls to the Preconditions class in Guava which use 'expensive' methods of producing the error string.ABugChecker; see the summary.Makes sure that you are not extending a class that has @AutoValue as an annotation.ABugChecker; see the associatedBugPatternannotation for details.Flags fields which can be replaced with local variables.Finds fields which can be safely made static.ABugChecker; see the associatedBugPatternannotation for details.Matches the behaviour of javac's finally Xlint warning.ABugChecker; see the associatedBugPatternannotation for details.Detects usages ofFloat,DoubleSubject.isWithin(TOLERANCE).of(EXPECTED)where there are no other floating point values other thanEXPECTEDwith satisfy the assertion, butTOLERANCEis not zero.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Verifies that methods markedForOverrideare only called from the defining class.ABugChecker; see the associatedBugPatternannotation for details.See BugPattern annotation.Checks for calls to Guava'sFutures.getCheckedmethod that will always fail because they pass an incompatible exception type.See summary for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Flags a few ways in which member names may violate the style guide.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation 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 finalImmutableSetoverImmutableListwhen 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 associatedBugPatternannotation for details.Checker for variables under the same scope that only differ in capitalization.Looks for hashCode implementations which are inconsistent with equals.See BugPattern annotation.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Bugpattern to encourage initializing effectively final variables inline with their declaration, if possible.A BugPattern; see the summary.ABugChecker; see the associatedBugPatternannotation 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 anInterruptedExceptionis caught as part of a catch block catching a supertype, and not specially handled.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Validates ZoneId.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Detects floating-point assertEquals() calls that will not work in JUnit 4.A bugpattern; see the associated summary.@BeforeClassor@AfterClassshould be applied to static methods.Deletes empty JUnit4@Before,@After,@BeforeClass, and@AfterClassmethods.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.Parametersannotation to provide parameters exists.A BugPattern; see the summary.Points out if #valueOf() is called on a Protocol Buffer Enum.Flags calls totoStringon lite protos.Suggests that calls toLock.lockmust be immediately followed by atry-finallythat 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 associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Matcher for alongliteral with a lower-case ell for a suffix (e.g.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.See the summary.A BugPattern; see the summary.See the summary.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.Checks for calls toDescriptor#findFieldByNumberwith 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 summaryA BugPattern; see the summary.ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Checks if a constructor or method annotated withMustBeClosedis called within the resource variable initializer of a try-with-resources statement.ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Discourages the use of negative boolean names.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Checks methods annotated with@NoAllocationto 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 associatedBugPatternannotation for details.A bugpattern; see the summary.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Flags passing literal null toOptional-accepting APIs.ABugChecker; see the associatedBugPatternannotation for details.Check for usage ofObjects.equalon primitive types.Check for calls to Objects'Objects.hashCode(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 associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.MatchesOptional#mapmapping to anotherOptional.ReplacesOptional.mapwithOptional.ifPresentif the value is unused.Checks ifOptional#ofis chained with a redundant method.ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation 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.A BugPattern; see the summary.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'signoringFieldsis passed field numbers from the correct proto.Checks if public APIs named "stream" returns a type whose name ends with Stream.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.A bug checker; see the summary.Removes overrides which purely pass through to the method in the super class.A BugPattern; see the summary.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Utility methods to find replacement variables with similar names.Check for non-allowlisted callers to RestrictedApiChecker.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Flags methods with collection return types which returnnullin 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.A BugPattern; see the summary.ABugChecker; see the associatedBugPatternfor details.Looks for types being shadowed by other types in a way that may be confusing.ABugChecker; see the associatedBugPatternannotation 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.A BugPattern; see the summary.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() >= 0orT[].length > 0.Checks for statement switches that can be converted into a new-style arrow (`->`) 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 associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Discouragestream::iteratorto createIterables.ABugChecker; see the associatedBugPatternannotation for details.ABugCheckerthat flags calls toString.toLowerCase()andString.toUpperCase(), as these methods implicitly rely on the environment's default locale.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Helper for strongly typing fields.Builder forStronglyTypeFlags fields which would be better expressed as ByteStrings rather than primitive byte arrays.Check for calls to String'sfoo.substring(0).ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Find uses of SuppressWarnings with "deprecated".Finds occurrences of@SuppressWarningswhere there is definitely no explanation for why it is safe.ABugChecker; see the associatedBugPatternannotation for details.Flagsjavax.lang.model.element.Element#toStringusage inBugCheckers.ABugChecker; see the associatedBugPatternannotation for details.Check for calls toSystem.exit()outside of a main method.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Flags uses of parameters in non-parameterized tests.Flags uses of@Theory(and others) in non-Theories-run tests.A check that bans using placeholders inThread.Builder.name(String)andThread.Builder.name(String, int).ABugChecker; see the associatedBugPatternannotation for details.A BugPattern; see the summary.A BugPattern; see the summary.ABugChecker; see the associatedBugPatternannotation for details.Bugpattern to discourage throwing base exception classes.Suggests to remove the unchecked throws clause.ABugChecker; see the associatedBugPatternannotation for details.ToString should not return null.ABugChecker; see the associatedBugPatternannotation for details.Warns against use of bothstaticandtransientmodifiers on field declarations.Flagscom.sun.source.tree.Tree#toStringusage inBugCheckers.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 associatedBugPatternannotation for details.Flags ambiguous usages ofMap#getOrDefaultwithinTruth#assertThat.ABugChecker; see the associatedBugPatternannotation for details.A bug checker for the following code pattern:ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.Enumerates types which have poorly-defined behaviour for equals.Flagsjavax.lang.model.type.TypeMirror#toStringusage inBugCheckers.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 associatedBugPatternannotation 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 associatedBugPatternannotation for details.A BugPattern; see the summary.ABugChecker; see the associatedBugPatternannotation for details.Removesfinalfrom non-field variables.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.A BugPattern; see the summary.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.See the summary.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Flags unsafe usages of theLocaleconstructor and class methods.Checks unsafe instance creation via reflection.ABugChecker; see the associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.ABugChecker; see the associatedBugPatternannotation for details.Check for variables and types with the same nameConverts some local variables to usevar.ABugChecker; see the associatedBugPatternannotation for details.A BugPattern; see the summary.A collection of annotations to treat as if they were annotated with@Keep.ABugChecker; see the associatedBugPatternannotation 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 associatedBugPatternannotation for details.See the summary.