Skip to content

"clippy::single_component_path_imports" False positive on swapped namespaces to enable mocking. #11925

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

Open
vnermolaev opened this issue Dec 4, 2023 · 0 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

@vnermolaev
Copy link

vnermolaev commented Dec 4, 2023

Summary

Imagine you have a 3rd part dependency on module module3.
My component depends on an exported member from that namespace, for example, module3::Foo.
I want to test my component's internal logic without instantiating module3::Foo.
To do so, I mock module3::Foo with mockall. I will now use module3 for actual builds, while use mock::module3 as module3 for test builds.
Lint single_component_path_imports is going to complain about my first use.

Lint Name

single_component_path_imports

Reproducer

I tried this code:

#[cfg(not(test))]
use module3;

#[cfg(test)]
use mock::module3 as module3;

fn computation(foo: module3::Foo) -> u32 { .. }

fn run_computation() {
  let foo =  module3::Foo::new();
  let c = computation(foo);
}

#[cfg(test)]
fn run_test_computation(foo: module::Foo) {
  let foo =  module3::Foo::new();
  let c = computation(foo);
  assert_eq!(c, 100);
}

I saw this happen:

warning: this import is redundant
  --> main.rs
   |
   | use module3;
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove it entirely
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_component_path_imports

I do not know if it is reasonable to expect the lint to deduce that the namespaces are swapped. However it definitely produces a warning where I did not expect it.

Version

rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: aarch64-apple-darwin
release: 1.74.0
LLVM version: 17.0.4

Additional Labels

No response

@vnermolaev vnermolaev added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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

1 participant