-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Warn the user about side effects when creating empty arrays and vectors #6439
Comments
This is a change in behavior; if there's a suggestion at all, it should be |
I agree that it's a change in behavior, but IMO there's precedent in clippy for recommending similar changes: clippy recommends closure-style APIs for things like But I recognize that this is a different case; just wanted to point out a potential parallel 😄 |
…side-effects, r=dtolnay doc(array,vec): add notes about side effects when empty-initializing Copying some context from a conversation in the Rust discord: * Both `vec![T; 0]` and `[T; 0]` are syntactically valid, and produce empty containers of their respective types * Both *also* have side effects: ```rust fn side_effect() -> String { println!("side effect!"); "foo".into() } fn main() { println!("before!"); let x = vec![side_effect(); 0]; let y = [side_effect(); 0]; println!("{:?}, {:?}", x, y); } ``` produces: ``` before! side effect! side effect! [], [] ``` This PR just adds two small notes to each's documentation, warning users that side effects can occur. I've also submitted a clippy proposal: rust-lang/rust-clippy#6439
…side-effects, r=dtolnay doc(array,vec): add notes about side effects when empty-initializing Copying some context from a conversation in the Rust discord: * Both `vec![T; 0]` and `[T; 0]` are syntactically valid, and produce empty containers of their respective types * Both *also* have side effects: ```rust fn side_effect() -> String { println!("side effect!"); "foo".into() } fn main() { println!("before!"); let x = vec![side_effect(); 0]; let y = [side_effect(); 0]; println!("{:?}, {:?}", x, y); } ``` produces: ``` before! side effect! side effect! [], [] ``` This PR just adds two small notes to each's documentation, warning users that side effects can occur. I've also submitted a clippy proposal: rust-lang/rust-clippy#6439
@rustbot claim |
What it does
Both the
vec![]
macro and the array bulk-initializer syntax ([T; N]
) allow the programmer to initialize an empty container, i.e. one that contains no copies ofT
.However, both syntaxes still invoke
T
at least once, meaning that code like this:can have unexpected side effects when
something
is called.Here's a proof of concept with both a vector and an array:
produces:
What does this lint do?
Warns the user that empty initializations of vectors and arrays can cause side effects.
Categories (optional)
Justification:
T
's initialization is useless, since the user can't access it (they're declaring an empty container, after all) and it ends up being dropped immediately anyways.What is the advantage of the recommended code over the original code
Avoids unexpected side effects and avoids a useless allocation.
Drawbacks
None.
Example
Could be written as:
The text was updated successfully, but these errors were encountered: