rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.31k stars 1.53k forks source link

cargo clippy report wrong 'the borrowed expression implements the required traits' #11156

Open Sherlock-Holo opened 1 year ago

Sherlock-Holo commented 1 year ago

Summary

cargo clippy report a warn message

Checking nix v0.26.2
Checking tracing-subscriber v0.3.17
Checking rtnetlink v0.13.0
Checking mahiro v0.1.0 (/home/sherlock/git/mahiro/mahiro)
warning: the borrowed expression implements the required traits --> mahiro/src/mihari/nat/mod.rs:301:39 301 map.insert(index, &addr.into(), 0) ^^^^^^^^^^^^ help: change this to: addr.into()
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
= note: `#[warn(clippy::needless_borrow)]` on by default
warning: the borrowed expression implements the required traits --> mahiro/src/mihari/nat/mod.rs:310:39 310 map.insert(index, &addr.into(), 0) ^^^^^^^^^^^^ help: change this to: addr.into()
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

warning: mahiro (lib) generated 2 warnings (run cargo clippy --fix --lib -p mahiro to apply 2 suggestions)

when running the cargo clippy --fix --lib -p mahiro, it reports

Checking mahiro v0.1.0 (/home/sherlock/git/mahiro/mahiro)

warning: failed to automatically apply fixes suggested by rustc to crate mahiro

after fixes were automatically applied the compiler reported errors within these files:

  • mahiro/src/mihari/mod.rs
  • mahiro/src/mihari/nat/mod.rs

This likely indicates a bug in either rustc or cargo itself, and we would appreciate a bug report! You're likely to see a number of compiler warnings after this message which cargo attempted to fix but failed. If you could open an issue at https://github.com/rust-lang/rust-clippy/issues quoting the full output of this command we'd be very appreciative! Note that you may be able to make some more progress in the near-term fixing code with the --broken-code flag

The following errors were reported: error[E0282]: type annotations needed --> mahiro/src/mihari/nat/mod.rs:301:44 301 map.insert(index, addr.into(), 0) ^^^^

help: try using a fully qualified path to specify the expected types | 301 | map.insert(index, <std::net::Ipv4Addr as std::convert::Into>::into(addr), 0) | ++++++++++++++++++++++++++++++++++++++++++++++++++++ ~

error[E0283]: type annotations needed --> mahiro/src/mihari/nat/mod.rs:301:44 301 map.insert(index, addr.into(), 0) ------ ^^^^
required by a bound introduced by this call
= note: multiple `impl`s satisfying `_: std::borrow::Borrow<mihari::nat::ip_addr::BpfIpv4Addr>` found in the following crates: `alloc`, `allocator_api2`, `core`:
        - impl<'a, B> std::borrow::Borrow<B> for std::borrow::Cow<'a, B>
          where B: std::borrow::ToOwned, B: ?Sized;
        - impl<T, A> std::borrow::Borrow<T> for allocator_api2::stable::boxed::Box<T, A>
          where A: allocator_api2::stable::alloc::Allocator, T: ?Sized;
        - impl<T, A> std::borrow::Borrow<T> for std::boxed::Box<T, A>
          where A: std::alloc::Allocator, T: ?Sized;
        - impl<T> std::borrow::Borrow<T> for &T
          where T: ?Sized;
        - impl<T> std::borrow::Borrow<T> for &mut T
          where T: ?Sized;
        - impl<T> std::borrow::Borrow<T> for T
          where T: ?Sized;
        - impl<T> std::borrow::Borrow<T> for std::rc::Rc<T>
          where T: ?Sized;
        - impl<T> std::borrow::Borrow<T> for std::sync::Arc<T>
          where T: ?Sized;
note: required by a bound in aya::maps::HashMap::<T, K, V>::insert --> /home/sherlock/.cargo/git/checkouts/aya-c55fbc69175ac116/5c86b7e/aya/src/maps/hash_map/hash_map.rs:84:21 81 pub fn insert( ------ required by a bound in this associated function ... 84 value: impl Borrow, ^^^^^^^^^ required by this bound in HashMap::<T, K, V>::insert help: try using a fully qualified path to specify the expected types
301 map.insert(index, <std::net::Ipv4Addr as std::convert::Into>::into(addr), 0)
++++++++++++++++++++++++++++++++++++++++++++++++++++ ~
error[E0283]: type annotations needed --> mahiro/src/mihari/nat/mod.rs:310:44 310 map.insert(index, addr.into(), 0) ------ ^^^^
required by a bound introduced by this call
= note: multiple `impl`s satisfying `_: std::borrow::Borrow<mihari::nat::ip_addr::BpfIpv6Addr>` found in the following crates: `alloc`, `allocator_api2`, `core`:
        - impl<'a, B> std::borrow::Borrow<B> for std::borrow::Cow<'a, B>
          where B: std::borrow::ToOwned, B: ?Sized;
        - impl<T, A> std::borrow::Borrow<T> for allocator_api2::stable::boxed::Box<T, A>
          where A: allocator_api2::stable::alloc::Allocator, T: ?Sized;
        - impl<T, A> std::borrow::Borrow<T> for std::boxed::Box<T, A>
          where A: std::alloc::Allocator, T: ?Sized;
        - impl<T> std::borrow::Borrow<T> for &T
          where T: ?Sized;
        - impl<T> std::borrow::Borrow<T> for &mut T
          where T: ?Sized;
        - impl<T> std::borrow::Borrow<T> for T
          where T: ?Sized;
        - impl<T> std::borrow::Borrow<T> for std::rc::Rc<T>
          where T: ?Sized;
        - impl<T> std::borrow::Borrow<T> for std::sync::Arc<T>
          where T: ?Sized;
note: required by a bound in aya::maps::HashMap::<T, K, V>::insert --> /home/sherlock/.cargo/git/checkouts/aya-c55fbc69175ac116/5c86b7e/aya/src/maps/hash_map/hash_map.rs:84:21 81 pub fn insert( ------ required by a bound in this associated function ... 84 value: impl Borrow, ^^^^^^^^^ required by this bound in HashMap::<T, K, V>::insert help: try using a fully qualified path to specify the expected types
310 map.insert(index, <std::net::Ipv6Addr as std::convert::Into>::into(addr), 0)
++++++++++++++++++++++++++++++++++++++++++++++++++++ ~
error: future cannot be sent between threads safely --> mahiro/src/mihari/mod.rs:180:28 180 join_set.spawn(async move { nat_actor.run().await }); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ future created by async block is not Send
note: opaque type is declared here --> mahiro/src/mihari/nat/mod.rs:261:45 261 async fn update_nic_addrs(&mut self) -> anyhow::Result<()> { ^^^^^^^^^^^^^^^^^^ note: this item depends on auto traits of the hidden type, but may also be registering the hidden type. This is not supported right now. You can try moving the opaque type and the item that actually registers a hidden type into a new submodule --> mahiro/src/mihari/mod.rs:32:14
32 pub async fn run(config: &Path, bpf_nat: bool) -> anyhow::Result<()> {
^^^
note: future is not Send as it awaits another future which is not Send --> mahiro/src/mihari/nat/mod.rs:258:9 258 self.update_nic_addrs().await ^^^^^^^^^^^^^^^^^^^^^^^ await occurs here on type impl futures_util::Future<Output = std::result::Result<(), anyhow::Error>>, which is not Send note: required by a bound in tokio::task::JoinSet::<T>::spawn --> /home/sherlock/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.29.1/src/task/join_set.rs:136:12
133 pub fn spawn(&mut self, task: F) -> AbortHandle
----- required by a bound in this associated function

... 136 | F: Send + 'static, | ^^^^ required by this bound in JoinSet::<T>::spawn

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0282, E0283. For more information about an error, try rustc --explain E0282. Original diagnostics will follow.

warning: the borrowed expression implements the required traits --> mahiro/src/mihari/nat/mod.rs:301:39 301 map.insert(index, &addr.into(), 0) ^^^^^^^^^^^^ help: change this to: addr.into()
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
= note: `#[warn(clippy::needless_borrow)]` on by default
warning: the borrowed expression implements the required traits --> mahiro/src/mihari/nat/mod.rs:310:39 310 map.insert(index, &addr.into(), 0) ^^^^^^^^^^^^ help: change this to: addr.into()
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

warning: mahiro (lib) generated 2 warnings (run cargo clippy --fix --lib -p mahiro to apply 2 suggestions) Finished dev [unoptimized + debuginfo] target(s) in 12.83s

Lint Name

clippy::needless_borrow

Reproducer

my project can reproduce this problem:

  1. clone the project and switch to specify commit
  2. cd mahiro dir
  3. run cargo clippy

I am using nightly version toolchain

Version

rustc 1.73.0-nightly (7bd81ee19 2023-07-13)
binary: rustc
commit-hash: 7bd81ee1902c049691d0a1f03be5558bee51d100
commit-date: 2023-07-13
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5

Additional Labels

No response

y21 commented 1 year ago

minimal reproducible example:

use std::borrow::Borrow;
use std::net::Ipv4Addr;

fn f<T>(_: impl Borrow<T>) -> T { todo!() }

fn main() {
  let _: Ipv4Addr = f(&[1, 2, 3, 4].into());
}
smoelius commented 1 year ago

Error message for the above:


error[[E0282]](https://doc.rust-lang.org/stable/error_codes/E0282.html): type annotations needed
 --> src/main.rs:9:38
  |
9 |     let _: Ipv4Addr = f([1, 2, 3, 4].into());
  |                                      ^^^^
  |
help: try using a fully qualified path to specify the expected types
  |
9 |     let _: Ipv4Addr = f(<[i32; 4] as Into<T>>::into([1, 2, 3, 4]));
  |                         ++++++++++++++++++++++++++++            ~

error[[E0283]](https://doc.rust-lang.org/stable/error_codes/E0283.html): type annotations needed
 --> src/main.rs:9:38
  |
9 |     let _: Ipv4Addr = f([1, 2, 3, 4].into());
  |                       -              ^^^^
  |                       |
  |                       required by a bound introduced by this call
  |
  = note: multiple `impl`s satisfying `_: Borrow<Ipv4Addr>` found in the following crates: `alloc`, `core`:
          - impl<'a, B> Borrow<B> for Cow<'a, B>
            where B: ToOwned, B: ?Sized;
          - impl<T, A> Borrow<T> for Box<T, A>
            where A: Allocator, T: ?Sized;
          - impl<T> Borrow<T> for &T
            where T: ?Sized;
          - impl<T> Borrow<T> for &mut T
            where T: ?Sized;
          - impl<T> Borrow<T> for Arc<T>
            where T: ?Sized;
          - impl<T> Borrow<T> for Rc<T>
            where T: ?Sized;
          - impl<T> Borrow<T> for T
            where T: ?Sized;
note: required by a bound in `f`
 --> src/main.rs:4:17
  |
4 | fn f<T>(_: impl Borrow<T>) -> T {
  |                 ^^^^^^^^^ required by this bound in `f`
help: try using a fully qualified path to specify the expected types
  |
9 |     let _: Ipv4Addr = f(<[i32; 4] as Into<T>>::into([1, 2, 3, 4]));
  |                         ++++++++++++++++++++++++++++            ~```