Class AbstractReturnValueIgnored

java.lang.Object
com.google.errorprone.bugpatterns.BugChecker
com.google.errorprone.bugpatterns.AbstractReturnValueIgnored
All Implemented Interfaces:
BugChecker.MemberReferenceTreeMatcher, BugChecker.MethodInvocationTreeMatcher, BugChecker.NewClassTreeMatcher, BugChecker.ReturnTreeMatcher, ResultUsePolicyAnalyzer<com.sun.source.tree.ExpressionTree,VisitorState>, Suppressible, Serializable
Direct Known Subclasses:
CheckReturnValue, FutureReturnValueIgnored, IgnoredPureGetter, ProtoBuilderReturnValueIgnored, RectIntersectReturnValueIgnored, ReturnValueIgnored, RxReturnValueIgnored

An abstract base class to match API usages in which the return value is not used.

In addition to regular contexts in which a return value isn't used (e.g.: the result of String.trim() is just ignored), this class has the capacity to determine if the result is cast in such a way as to lose important static type information.

If an analysis extending this base class chooses to care about this circumstance, they can override lostType(com.google.errorprone.VisitorState) to define the type information they wish to keep.

Author:
eaftan@google.com (Eddie Aftandilian)
See Also:
  • Constructor Details

    • AbstractReturnValueIgnored

      protected AbstractReturnValueIgnored()
    • AbstractReturnValueIgnored

      protected AbstractReturnValueIgnored(ConstantExpressions constantExpressions)
  • Method Details

    • matchMethodInvocation

      public Description matchMethodInvocation(com.sun.source.tree.MethodInvocationTree methodInvocationTree, VisitorState state)
      Specified by:
      matchMethodInvocation in interface BugChecker.MethodInvocationTreeMatcher
    • matchNewClass

      public Description matchNewClass(com.sun.source.tree.NewClassTree newClassTree, VisitorState state)
      Specified by:
      matchNewClass in interface BugChecker.NewClassTreeMatcher
    • matchMemberReference

      public Description matchMemberReference(com.sun.source.tree.MemberReferenceTree tree, VisitorState state)
      Specified by:
      matchMemberReference in interface BugChecker.MemberReferenceTreeMatcher
    • isCovered

      public boolean isCovered(com.sun.source.tree.ExpressionTree tree, VisitorState state)
      Description copied from interface: ResultUsePolicyAnalyzer
      Returns whether this checker makes any determination about whether the given expression's return value should be used or not. Most checkers either determine that an expression is CRV or make no determination.
      Specified by:
      isCovered in interface ResultUsePolicyAnalyzer<com.sun.source.tree.ExpressionTree,VisitorState>
    • getMethodPolicy

      public ResultUsePolicy getMethodPolicy(com.sun.source.tree.ExpressionTree expression, VisitorState state)
      Description copied from interface: ResultUsePolicyAnalyzer
      Returns the ResultUsePolicy for the method used in the given expression.
      Specified by:
      getMethodPolicy in interface ResultUsePolicyAnalyzer<com.sun.source.tree.ExpressionTree,VisitorState>
    • specializedMatcher

      protected abstract Matcher<? super com.sun.source.tree.ExpressionTree> specializedMatcher()
      Match whatever additional conditions concrete subclasses want to match (a list of known side-effect-free methods, has a @CheckReturnValue annotation, etc.).
    • lostType

      protected Optional<com.sun.tools.javac.code.Type> lostType(VisitorState state)
      Check for occurrences of this type being lost, i.e. cast to Object.
    • lostTypeMessage

      protected String lostTypeMessage(String returnedType, String declaredReturnType)
    • allowInExceptionThrowers

      protected boolean allowInExceptionThrowers()
      Override this to return false to forbid discarding return values in testers that are testing whether an exception is thrown.
    • describeReturnValueIgnored

      protected Description describeReturnValueIgnored(com.sun.source.tree.MethodInvocationTree methodInvocationTree, VisitorState state)
      Fixes the error by assigning the result of the call to the receiver reference, or deleting the method call. Subclasses may override if they prefer a different description.
    • describeReturnValueIgnored

      protected Description describeReturnValueIgnored(com.sun.source.tree.MemberReferenceTree memberReferenceTree, VisitorState state)
      Uses the default description for results ignored via a method reference. Subclasses may override if they prefer a different description.
    • describeReturnValueIgnored

      protected Description describeReturnValueIgnored(com.sun.source.tree.NewClassTree newClassTree, VisitorState state)
      Uses the default description for results ignored via a constructor call. Subclasses may override if they prefer a different description.
    • getMessage

      protected String getMessage(Name name)
      Returns the diagnostic message. Can be overridden by subclasses to provide a customized diagnostic that includes the name of the invoked method.
    • matchReturn

      public Description matchReturn(com.sun.source.tree.ReturnTree tree, VisitorState state)
      Returning a type from a lambda or method that returns Object loses the type information.
      Specified by:
      matchReturn in interface BugChecker.ReturnTreeMatcher