Skip to content

Commit 4dd2252

Browse files
committed
Fix suggestion for weird formattings
1 parent b562a51 commit 4dd2252

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

clippy_lints/src/wildcard_imports.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{in_macro, snippet_with_applicability, span_lint_and_sugg};
1+
use crate::utils::{in_macro, snippet, snippet_with_applicability, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc_errors::Applicability;
44
use rustc_hir::{
@@ -46,6 +46,9 @@ declare_clippy_lint! {
4646
/// **Known problems:** If macros are imported through the wildcard, this macro is not included
4747
/// by the suggestion and has to be added by hand.
4848
///
49+
/// Applying the suggestion when explicit imports of the things imported with a glob import
50+
/// exist, may result in `unused_imports` warnings.
51+
///
4952
/// **Example:**
5053
///
5154
/// Bad:
@@ -82,16 +85,27 @@ impl LateLintPass<'_, '_> for WildcardImports {
8285
if !used_imports.is_empty(); // Already handled by `unused_imports`
8386
then {
8487
let mut applicability = Applicability::MachineApplicable;
85-
let import_source = snippet_with_applicability(cx, use_path.span, "..", &mut applicability);
86-
let (span, braced_glob) = if import_source.is_empty() {
88+
let import_source_snippet = snippet_with_applicability(cx, use_path.span, "..", &mut applicability);
89+
let (span, braced_glob) = if import_source_snippet.is_empty() {
8790
// This is a `_::{_, *}` import
91+
// In this case `use_path.span` is empty and ends directly in front of the `*`,
92+
// so we need to extend it by one byte.
8893
(
8994
use_path.span.with_hi(use_path.span.hi() + BytePos(1)),
9095
true,
9196
)
9297
} else {
98+
// In this case, the `use_path.span` ends right before the `::*`, so we need to
99+
// extend it up to the `*`. Since it is hard to find the `*` in weird
100+
// formattings like `use _ :: *;`, we extend it up to, but not including the
101+
// `;`. In nested imports, like `use _::{inner::*, _}` there is no `;` and we
102+
// can just use the end of the item span
103+
let mut span = use_path.span.with_hi(item.span.hi());
104+
if snippet(cx, span, "").ends_with(';') {
105+
span = use_path.span.with_hi(item.span.hi() - BytePos(1));
106+
}
93107
(
94-
use_path.span.with_hi(use_path.span.hi() + BytePos(3)),
108+
span,
95109
false,
96110
)
97111
};
@@ -111,10 +125,10 @@ impl LateLintPass<'_, '_> for WildcardImports {
111125
}
112126
};
113127

114-
let sugg = if import_source.is_empty() {
128+
let sugg = if braced_glob {
115129
imports_string
116130
} else {
117-
format!("{}::{}", import_source, imports_string)
131+
format!("{}::{}", import_source_snippet, imports_string)
118132
};
119133

120134
let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res {

tests/ui/wildcard_imports.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,12 @@ fn test_reexported() {
145145
let _ = ExportedStruct;
146146
let _ = ExportedEnum::A;
147147
}
148+
149+
#[rustfmt::skip]
150+
fn test_weird_formatting() {
151+
use crate:: in_fn_test::exported;
152+
use crate:: fn_mod::foo;
153+
154+
exported();
155+
foo();
156+
}

tests/ui/wildcard_imports.rs

+10
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,13 @@ fn test_reexported() {
145145
let _ = ExportedStruct;
146146
let _ = ExportedEnum::A;
147147
}
148+
149+
#[rustfmt::skip]
150+
fn test_weird_formatting() {
151+
use crate:: in_fn_test:: * ;
152+
use crate:: fn_mod::
153+
*;
154+
155+
exported();
156+
foo();
157+
}

tests/ui/wildcard_imports.stderr

+15-1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,19 @@ error: usage of wildcard import
7878
LL | use crate::in_fn_test::*;
7979
| ^^^^^^^^^^^^^^^^^^^^ help: try: `crate::in_fn_test::{ExportedEnum, ExportedStruct, exported}`
8080

81-
error: aborting due to 13 previous errors
81+
error: usage of wildcard import
82+
--> $DIR/wildcard_imports.rs:151:9
83+
|
84+
LL | use crate:: in_fn_test:: * ;
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `crate:: in_fn_test::exported`
86+
87+
error: usage of wildcard import
88+
--> $DIR/wildcard_imports.rs:152:9
89+
|
90+
LL | use crate:: fn_mod::
91+
| _________^
92+
LL | | *;
93+
| |_________^ help: try: `crate:: fn_mod::foo`
94+
95+
error: aborting due to 15 previous errors
8296

0 commit comments

Comments
 (0)