Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

op_ref false positive #2042

Open
euclio opened this issue Sep 11, 2017 · 3 comments
Open

op_ref false positive #2042

euclio opened this issue Sep 11, 2017 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@euclio
Copy link
Contributor

euclio commented Sep 11, 2017

With the following code: https://play.rust-lang.org/?gist=1aa5789d7082019e91bac7b093304ad5&version=stable

Clippy suggests removing the reference, but without the reference the Rust compiler complains that type annotations are needed.

I would expect the lint to not fire in this case.

@oli-obk
Copy link
Contributor

oli-obk commented Sep 11, 2017

That is so weird. I think this is rather a rustc bug... I'll investigate

@arielb1
Copy link

arielb1 commented Sep 12, 2017

That's a trait-system issue. Operator overloading does succeed in this case. Without the autoref, you get these trait bounds:

u16: Add<$0>
u8: Into<$0>

Because the compiler only considers each trait bound alone, it can't see that the only solution is $0 = u16.

If you add a reference, instead yoou get

u16: Add<&'a $0>
u8: Into<$0>

And on the first bound, only the impl Add<&u16> for u16 impl matches, which allows a solution.

@oli-obk oli-obk added the C-bug Category: Clippy is not doing the correct thing label Apr 2, 2018
@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 21, 2020
@samueltardieu
Copy link
Contributor

For reference, -Znext-solver gives a more precise diagnostic about the eligible trait implementations but still requires an explicit annotation:

error[E0283]: type annotations needed
 --> /tmp/t.rs:5:25
  |
5 |     let x = word + byte.into();
  |                  -      ^^^^
  |                  |
  |                  type must be known at this point
  |
  = note: multiple `impl`s satisfying `u16: std::ops::Add<_>` found in the `core` crate:
          - impl std::ops::Add for u16;
          - impl std::ops::Add<&u16> for u16;
help: try using a fully qualified path to specify the expected types
  |
5 |     let x = word + <u8 as std::convert::Into<T>>::into(byte);
  |                    ++++++++++++++++++++++++++++++++++++    ~

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

5 participants