Skip to content

Confusing error message when applying binary operation on the wrong type #33877

Closed
@gnzlbg

Description

@gnzlbg

The following snippet:

let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
let vr = v.iter().filter(|x| x % 2 == 0); // wrong
// let vr = v.iter().filter(|&x| x % 2 == 0); // correct

produces the following error:

<anon>:10:34: 10:35 error: binary operation `%` cannot be applied to type `&&_` [E0369]
<anon>:10     let vr = v.iter().filter(|x| x % 2 == 0);
                                           ^
<anon>:10:34: 10:35 help: see the detailed explanation for E0369
<anon>:10:34: 10:35 note: an implementation of `std::ops::Rem` might be missing for `&&_`
<anon>:10     let vr = v.iter().filter(|x| x % 2 == 0);
                                           ^
error: aborting due to previous error

How is anybody supposed to extract from binary operation % cannot be applied to type &&_ that one forgot the &x in the closure? The explanation E0369 doesn't help here.

Maybe a better thing for rustc would be to "try" and see if a combination of & and &mut would have solved the issue and offer a suggestion of the form "Did you meant to make x a reference here?".

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions