Skip to content

Add unstable #[may_ignore] attribute to cancel #[must_use] #79572

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,9 @@ declare_features! (
/// Allows capturing disjoint fields in a closure/generator (RFC 2229).
(active, capture_disjoint_fields, "1.49.0", Some(53488), None),

/// Allow `#[may_ignore]` attribute.
(active, may_ignore, "1.50.0", Some(99999999), None),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// TODO: This still needs a real tracking issue.


// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
ungated!(forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
ungated!(deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
ungated!(must_use, AssumedUsed, template!(Word, NameValueStr: "reason")),
gated!(may_ignore, AssumedUsed, template!(Word), experimental!(may_ignore)),
// FIXME(#14407)
ungated!(
deprecated, Normal,
Expand Down
20 changes: 15 additions & 5 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
return;
}

let ty = cx.typeck_results().expr_ty(&expr);
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "", "", 1);

let mut fn_warned = false;
let mut op_warned = false;
let maybe_def_id = match expr.kind {
hir::ExprKind::Call(ref callee, _) => {
match callee.kind {
Expand All @@ -123,6 +118,21 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
hir::ExprKind::MethodCall(..) => cx.typeck_results().type_dependent_def_id(expr.hir_id),
_ => None,
};

if let Some(def_id) = maybe_def_id {
for attr in cx.tcx.get_attrs(def_id).iter() {
if cx.sess().check_name(attr, sym::may_ignore) {
// Don't warn if the function was marked as #[may_ignore].
return;
}
}
}

let ty = cx.typeck_results().expr_ty(&expr);
let type_permits_lack_of_use = check_must_use_ty(cx, ty, &expr, s.span, "", "", 1);

let mut fn_warned = false;
let mut op_warned = false;
if let Some(def_id) = maybe_def_id {
fn_warned = check_must_use_def(cx, def_id, s.span, "return value of ", "");
} else if type_permits_lack_of_use {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ symbols! {
maxnumf32,
maxnumf64,
may_dangle,
may_ignore,
maybe_uninit,
maybe_uninit_uninit,
maybe_uninit_zeroed,
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/feature-gate-may_ignore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[may_ignore]
//~^ ERROR the `#[may_ignore]` attribute is an experimental feature [E0658]
fn main() {
}
12 changes: 12 additions & 0 deletions src/test/ui/feature-gate-may_ignore.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0658]: the `#[may_ignore]` attribute is an experimental feature
--> $DIR/feature-gate-may_ignore.rs:1:1
|
LL | #[may_ignore]
| ^^^^^^^^^^^^^
|
= note: see issue #99999999 <https://github.com/rust-lang/rust/issues/99999999> for more information
= help: add `#![feature(may_ignore)]` to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.
18 changes: 18 additions & 0 deletions src/test/ui/may_ignore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// check-pass

#![feature(may_ignore)]
#![warn(unused_must_use)]

fn warn() -> Result<i32, i32> {
Err(1)
}

#[may_ignore]
fn no_warn() -> Result<i32, i32> {
Err(2)
}

fn main() {
warn(); //~ WARN [unused_must_use]
no_warn();
}
15 changes: 15 additions & 0 deletions src/test/ui/may_ignore.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
warning: unused `std::result::Result` that must be used
--> $DIR/may_ignore.rs:16:5
|
LL | warn();
| ^^^^^^^
|
note: the lint level is defined here
--> $DIR/may_ignore.rs:4:9
|
LL | #![warn(unused_must_use)]
| ^^^^^^^^^^^^^^^
= note: this `Result` may be an `Err` variant, which should be handled

warning: 1 warning emitted