Skip to content

Irritating "unused" warning #108885

Closed

Description

@FlorianDyck

Code

fn main() {
    let mut vec = vec![[0].iter(), [1].iter()];
    vec.remove(0);
}

Current output

warning: unused `std::slice::Iter` that must be used
  --> src\main.rs:52:5
   |
52 |     vec.remove(0);
   |     ^^^^^^^^^^^^^^
   |
   = note: iterators are lazy and do nothing unless consumed

Desired output

No warning when nothing is done to an iterator or similar when they are returned from a function with side effects.

If a warning is shown, it should be like this:

warning: unused return value.
You can use `let _ = vec.remove(0)` to signify that dropping it without usage is the intended behavior.

Rationale and extra context

rustc finds that remove returns an iterator.
Obviously I am not doing anything with it.
But the of the line is not to do something with the iterator, but to remove it from the list.

Other cases

No response

Anything else?

No response

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 8, 2023
  2. workingjubilee commented on Mar 8, 2023

    @workingjubilee
    Member

    You may silence the warning with let _ = vec.remove(0);

  3. added
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
    on Mar 8, 2023
  4. workingjubilee commented on Mar 8, 2023

    @workingjubilee
    Member

    This diagnostic should recommend that.

  5. FlorianDyck commented on Mar 8, 2023

    @FlorianDyck
    Author

    In this case I think the output could be improved to:

    warning: unused return value.
    You can use "let _ = ..." to signify that dropping it without usage is the intended behavior.

  6. Ezrashaw commented on Mar 11, 2023

    @Ezrashaw
    Contributor

    @rustbot claim

  7. added a commit that references this issue on Mar 16, 2023
    36b8237
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions