-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Annotation + a linter requiring using a return value #58084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
We do have Maybe a generalization of @dontDiscard
class C {}
@dontDiscard
int foo() => 42;
@dontDiscard
typedef F = int Function();
main() {
C(); // Lint: Don't discard an instance of `C`.
var c = C(); // OK, and backed up by hint: unused local variable.
foo(); // Lint: Don't discard the returned value from `foo`.
F f = foo;
f(); // Lint: Don't discard the returned value from a function of type `F`.
// And here's an interesting case:
void ignored;
ignored.toString(); // Error, you shouldn't use a value of type `void`.
ignored = 42; // OK; we could also execute `42;` as a statement.
ignored = f(); // Lint: Don't discard..
} |
Thanks for the suggestion @RobertSzymacha. I think something along the lines of errorprone's |
I thin you're describing the unnecessary_statements rule. See the tests for examples. |
Right, I can see a certain amount of overlap, but the The former needs to characterize certain elements (like But the |
Android has Would this belong in the meta package? |
Yes, I would expect the annotation to be added to the |
WRT naming, +1 to |
Annotation WIP: https://dart-review.googlesource.com/c/sdk/+/200301 |
/// Used to annotate a method, field, or getter within a class, mixin, or
/// extension, or a or top-level getter, variable or function to indicate that
/// the value obtained by invoking it should be use. A value is considered used
/// if it is assigned to a variable, passed to a function, or used as the target
/// of an invocation, or invoked (if the result is itself a function).
///
/// Tools, such as the analyzer, can provide feedback if
///
/// * the annotation is associated with anything other than a method, field or
/// getter, top-level variable, getter or function or
/// * the value obtained by a method, field, getter or top-level getter,
/// variable or function annotated with `@useResult` is not used.
const UseResult useResult = UseResult(); |
See: https://github.com/dart-lang/linter/issues/1888 Change-Id: Idd529c86236a4c2692104dc36cb0a7f0d5dd35aa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/200301 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
Implicit Ignored Contexts@RobertSzymacha (and others), as I work on this implementation, I'm curious where you weigh in on the various implicit "ignore contexts" described in the Java @CheckReturnValue docs. In particular it seems like we should consider some cases to allow for unused values in some test contexts. How do people feel about the Explicit Ignored ContextsWhat about the explicit pattern of suppression that involves assigning to a variable |
@rofrankel: would "ignored contexts" play into how you'd expect to use this annotation to support fluent programming w/ flute? Are there patterns we should be thinking about? |
See: https://github.com/dart-lang/linter/issues/1888 Change-Id: I3d5ed31f3a1dd0d69da5f47ae38496a4fe6051c0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/201160 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
Hi, is there any updates? |
Hi @fzyzcjy. Sorry btw for leaving this open. Let's close it in favor of targeted issues when they come up. Thanks! |
Describe the rule you'd like to see implemented
In Java there is an annotation for a linter https://errorprone.info/bugpattern/CheckReturnValue which warns engineers that the specific call is not useful if the value is ignored. This is used in cases where:
..
operator (see examples below)Examples
It is useful for instance when:
builtValue.rebuild((b) => b..someField = 'value')
w/o using the valueIn Dart it could be especially useful to catch following bug:
In that example, value of
rebuild()
is being ignored due to using..
operator (which is a bug), and one should use insteadIf the
rebuild()
method was annotated withCheckReturnValue
, linter should display a warning/error, that 'Value returned by rebuild() is not used'.Similarly, following code snipped is a clear bug as well, that would be easily prevented with that linter:
Typing
..
is very close to typing just.
, Dart would benefit from that linter even more than JavaThe text was updated successfully, but these errors were encountered: