Files
allwpilib/wpiannotations/src/main/java/org/wpilib/annotation/NoDiscard.java
Sam Carlberg 3972b01c51 Add javac plugin for detecting common error cases at compile time (#8196)
Adds a `@NoDiscard` annotation that can be placed on methods to guarantee their return values are used and on types to guarantee that any method returning that type uses the return value.

Methods that call `@NoDiscard`-annotated functions can add a `@SuppressWarnings("NoDiscard")` or `@SuppressWarnings("all")` annotation (or annotation on the class declaring that method) to silence the compiler error warnings.
2025-10-03 17:42:47 -07:00

28 lines
1.1 KiB
Java

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Marks a method as returning a value that must be used. The WPILib compiler plugin will check for
* uses of methods with this annotation and report a compiler error if the value is unused. Marking
* a class or interface as {@code @NoDiscard} will act as if any method that returns that type or
* any subclass or implementor of that type has been marked with {@code @NoDiscard}.
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.CLASS) // needs to be stored in the class for use by libraries
public @interface NoDiscard {
/**
* An error message to display if the return value is not used.
*
* @return The error message.
*/
String value() default "";
}