diff --git a/annotations/src/main/java/com/google/errorprone/annotations/CheckReturnValue.java b/annotations/src/main/java/com/google/errorprone/annotations/CheckReturnValue.java new file mode 100644 index 00000000000..5dd96c8a9c4 --- /dev/null +++ b/annotations/src/main/java/com/google/errorprone/annotations/CheckReturnValue.java @@ -0,0 +1,41 @@ +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.errorprone.annotations; + +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PACKAGE; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Indicates that the return value of the annotated method must be checked. An error is triggered + * when one of these methods is called but the result is not used. + * + *

{@code @CheckReturnValue} may be applied to a class or package to indicate that all methods in + * that class or package must have their return values checked. For convenience, we provide an + * annotation, {@link CanIgnoreReturnValue}, to exempt specific methods or classes from this + * behavior. + */ +@Documented +@Target({METHOD, CONSTRUCTOR, TYPE, PACKAGE}) +@Retention(RUNTIME) +public @interface CheckReturnValue {} diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/CheckReturnValue.java b/core/src/main/java/com/google/errorprone/bugpatterns/CheckReturnValue.java index 8687ccdd9e5..c7678846a3f 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/CheckReturnValue.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/CheckReturnValue.java @@ -120,8 +120,7 @@ public Matcher specializedMatcher() { "@CheckReturnValue and @CanIgnoreReturnValue cannot both be applied to the same %s"; /** - * Validate {@link javax.annotation.CheckReturnValue} and {@link CanIgnoreReturnValue} usage on - * methods. + * Validate {@code @CheckReturnValue} and {@link CanIgnoreReturnValue} usage on methods. * *

The annotations should not both be appled to the same method. * diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/CheckReturnValuePositiveCases.java b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/CheckReturnValuePositiveCases.java index 8b765b362d6..2e47451b4b5 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/CheckReturnValuePositiveCases.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/CheckReturnValuePositiveCases.java @@ -24,7 +24,7 @@ public class CheckReturnValuePositiveCases { IntValue intValue = new IntValue(0); - @CheckReturnValue + @javax.annotation.CheckReturnValue private int increment(int bar) { return bar + 1; } @@ -118,7 +118,7 @@ public IntValue(int i) { this.i = i; } - @CheckReturnValue + @javax.annotation.CheckReturnValue public IntValue increment() { return new IntValue(i + 1); } @@ -135,7 +135,7 @@ public void increment3() { } private static class MyObject { - @CheckReturnValue + @javax.annotation.CheckReturnValue MyObject() {} } @@ -143,7 +143,7 @@ private abstract static class LB1 {} private static class LB2 extends LB1 { - @CheckReturnValue + @javax.annotation.CheckReturnValue public static LB2 lb1() { return new LB2(); } @@ -154,4 +154,16 @@ public static LB2 lb2() { return lb1(); } } + + private static class ErrorProneAnnotation { + @com.google.errorprone.annotations.CheckReturnValue + public static int check() { + return 1; + } + + public static void ignoresCheck() { + // BUG: Diagnostic contains: remove this line + check(); + } + } }