Skip to content

Commit

Permalink
Add a version of @CheckReturnValue to ErrorProne
Browse files Browse the repository at this point in the history
RELNOTES: Add a version of @CheckReturnValue to ErrorProne

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=175564025
  • Loading branch information
ronshapiro authored and cushon committed Nov 15, 2017
1 parent b02e18a commit 7967165
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>{@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 {}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ public Matcher<ExpressionTree> 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.
*
* <p>The annotations should not both be appled to the same method.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class CheckReturnValuePositiveCases {

IntValue intValue = new IntValue(0);

@CheckReturnValue
@javax.annotation.CheckReturnValue
private int increment(int bar) {
return bar + 1;
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public IntValue(int i) {
this.i = i;
}

@CheckReturnValue
@javax.annotation.CheckReturnValue
public IntValue increment() {
return new IntValue(i + 1);
}
Expand All @@ -135,15 +135,15 @@ public void increment3() {
}

private static class MyObject {
@CheckReturnValue
@javax.annotation.CheckReturnValue
MyObject() {}
}

private abstract static class LB1<A> {}

private static class LB2<A> extends LB1<A> {

@CheckReturnValue
@javax.annotation.CheckReturnValue
public static <T> LB2<T> lb1() {
return new LB2<T>();
}
Expand All @@ -154,4 +154,16 @@ public static <T> LB2<T> 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();
}
}
}

0 comments on commit 7967165

Please sign in to comment.