-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Tracking issue for core::hint::must_use #94745
Comments
Copied over 2 proposed alternatives from #94723 (comment). I like |
I don't think it would be blocked on #15701 -- I believe exprs on blocks are currently permitted, e.g., cfg and lint attrs (allow etc) work: fn foo() -> i32 {
#[cfg(unix)]
#[allow(unused)]
{
println!("test");
let bar = 5;
3
}
} I am not familiar with the implementation details of must_use enough to know whether putting it on a block causes problems, though naively I would assume it should not. |
I'm somewhat concerned it might give an impression to a developer that fn my_function() -> u32 {
core::hint::must_use(42)
} is equivalent to #[must_use]
fn my_function() -> u32 {
42
} |
@Mark-Simulacrum what currently works is attributes on statements. In order for us to use let _ = #[allow(unused_must_use)] { 0 }; error[E0658]: attributes on expressions are experimental
--> src/lib.rs:2:13
|
2 | let _ = #[allow(unused_must_use)] { 0 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information |
Alternatively I suppose it would be possible to support attributes only on an outermost macro-produced expression/block, without going all the way to attributes on expressions in general, if that's less prone to grammatical ambiguities or whatever is holding up #15701. That is we could conceivably make the following work (which it doesn't today) without making the code in my previous comment work: macro_rules! repro {
() => {
#[allow(unused_must_use)] { 0 }
};
}
fn f() {
let _ = repro!();
} |
To clarify, what @Mark-Simulacrum shows is not a statement; its' the entire body of the function, including tail return. You can use this to put an attribute on the body of any block: fn main() {
let _ = { #[allow(unused_must_use)] 0 };
} pub fn is_foo_enabled() -> bool {
#[cfg(feature = "foo")] {
true
}
#[cfg(not(feature = "foo"))] {
false
}
} macro_rules! repro {
() => {
{
// does not create a lint; the outer block "uses" it, I suspect
#[must_use] {
let [some, complicated, expansion] = [(); 3];
let _ = [some, complicated, expansion];
5
}
}
};
}
pub fn f() {
repro!();
} |
All of what you showed are attributes on statements ( |
The reason macro_rules! make_error {
(...) => {
#[must_use]
{
let error = ...;
error
}
};
} in which case a call site macro_rules! make_error {
(...) => {
{
#[must_use]
{
let error = ...;
error
}
}
};
} in which case the macro_rules! repro {
() => {
{
f()
}
}
}
#[must_use]
fn f() -> i32 {
0
}
fn main() {
repro!(); // not used, but no warning
} |
I've been wondering about using this function in the #[lang = "must_use_fn"]
pub const fn must_use<T, const msg: &'static str>(value: T) -> T {
value
} This would require the |
I am that developer 😄 . What exactly is the difference between these two must-use code blocks? |
@wmmc88 the former attaches the lint to the specific site (so it only fires once), while latter attaches it to every site |
Possible unresolved question: #124493 |
Mark format! with must_use hint Uses unstable feature rust-lang#94745 Part of rust-lang#126475 First contribution to rust, please let me know if the blessing of tests is correct Thanks `@bjorn3` for the help
Rollup merge of rust-lang#127355 - aceArt-GmbH:126475, r=oli-obk Mark format! with must_use hint Uses unstable feature rust-lang#94745 Part of rust-lang#126475 First contribution to rust, please let me know if the blessing of tests is correct Thanks `@bjorn3` for the help
Mark format! with must_use hint Uses unstable feature rust-lang/rust#94745 Part of #126475 First contribution to rust, please let me know if the blessing of tests is correct Thanks `@bjorn3` for the help
Mark format! with must_use hint Uses unstable feature rust-lang/rust#94745 Part of #126475 First contribution to rust, please let me know if the blessing of tests is correct Thanks `@bjorn3` for the help
Mark format! with must_use hint Uses unstable feature rust-lang/rust#94745 Part of #126475 First contribution to rust, please let me know if the blessing of tests is correct Thanks `@bjorn3` for the help
Feature gate:
#![feature(hint_must_use)]
This is a tracking issue for the function
core::hint::must_use
Public API
Steps / History
Unresolved Questions
Alternative:
#[must_use]
on an arbitrary block or expression?This feels more native, and might behave slightly better in some subtle edge cases—type inference may not always 'see through' the identity function in terms of whether unsize coercions occur before or after the call, whereas through a block that isn't an issue.
Blocked on Tracking issue for stmt_expr_attributes: Add attributes to expressions, etc. #15701.
Alternative:
#[must_use]
integrated into macro_rules, perhaps on each arm, or on the whole macro, perhaps with a lint or error if the macro does not produce an expression.The text was updated successfully, but these errors were encountered: