Skip to content

Commit 27165ac

Browse files
committed
Auto merge of rust-lang#11456 - tom-anders:std_instead_of_core_suggestion, r=Manishearth
Add suggestions for std_instead_of_core ``` changelog: [`std_instead_of_core`]: add suggestions ``` Fixes rust-lang#11446
2 parents 253f1c4 + e0014af commit 27165ac

File tree

4 files changed

+101
-59
lines changed

4 files changed

+101
-59
lines changed

clippy_lints/src/std_instead_of_core.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use rustc_errors::Applicability;
23
use rustc_hir::def::Res;
34
use rustc_hir::def_id::DefId;
45
use rustc_hir::{HirId, Path, PathSegment};
@@ -99,17 +100,17 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
99100
&& let Some(first_segment) = get_first_segment(path)
100101
&& is_stable(cx, def_id)
101102
{
102-
let (lint, msg, help) = match first_segment.ident.name {
103+
let (lint, used_mod, replace_with) = match first_segment.ident.name {
103104
sym::std => match cx.tcx.crate_name(def_id.krate) {
104105
sym::core => (
105106
STD_INSTEAD_OF_CORE,
106-
"used import from `std` instead of `core`",
107-
"consider importing the item from `core`",
107+
"std",
108+
"core",
108109
),
109110
sym::alloc => (
110111
STD_INSTEAD_OF_ALLOC,
111-
"used import from `std` instead of `alloc`",
112-
"consider importing the item from `alloc`",
112+
"std",
113+
"alloc",
113114
),
114115
_ => {
115116
self.prev_span = path.span;
@@ -120,8 +121,8 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
120121
if cx.tcx.crate_name(def_id.krate) == sym::core {
121122
(
122123
ALLOC_INSTEAD_OF_CORE,
123-
"used import from `alloc` instead of `core`",
124-
"consider importing the item from `core`",
124+
"alloc",
125+
"core",
125126
)
126127
} else {
127128
self.prev_span = path.span;
@@ -131,7 +132,14 @@ impl<'tcx> LateLintPass<'tcx> for StdReexports {
131132
_ => return,
132133
};
133134
if path.span != self.prev_span {
134-
span_lint_and_help(cx, lint, path.span, msg, None, help);
135+
span_lint_and_sugg(
136+
cx,
137+
lint,
138+
first_segment.ident.span,
139+
&format!("used import from `{used_mod}` instead of `{replace_with}`"),
140+
&format!("consider importing the item from `{replace_with}`"),
141+
replace_with.to_string(),
142+
Applicability::MachineApplicable);
135143
self.prev_span = path.span;
136144
}
137145
}

tests/ui/std_instead_of_core.fixed

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#![warn(clippy::std_instead_of_core)]
2+
#![allow(unused_imports)]
3+
4+
extern crate alloc;
5+
6+
#[warn(clippy::std_instead_of_core)]
7+
fn std_instead_of_core() {
8+
// Regular import
9+
use core::hash::Hasher;
10+
//~^ ERROR: used import from `std` instead of `core`
11+
// Absolute path
12+
use ::core::hash::Hash;
13+
//~^ ERROR: used import from `std` instead of `core`
14+
// Don't lint on `env` macro
15+
use std::env;
16+
17+
// Multiple imports
18+
use core::fmt::{Debug, Result};
19+
//~^ ERROR: used import from `std` instead of `core`
20+
21+
// Function calls
22+
let ptr = core::ptr::null::<u32>();
23+
//~^ ERROR: used import from `std` instead of `core`
24+
let ptr_mut = ::core::ptr::null_mut::<usize>();
25+
//~^ ERROR: used import from `std` instead of `core`
26+
27+
// Types
28+
let cell = core::cell::Cell::new(8u32);
29+
//~^ ERROR: used import from `std` instead of `core`
30+
let cell_absolute = ::core::cell::Cell::new(8u32);
31+
//~^ ERROR: used import from `std` instead of `core`
32+
33+
let _ = std::env!("PATH");
34+
35+
// do not lint until `error_in_core` is stable
36+
use std::error::Error;
37+
38+
// lint items re-exported from private modules, `core::iter::traits::iterator::Iterator`
39+
use core::iter::Iterator;
40+
//~^ ERROR: used import from `std` instead of `core`
41+
}
42+
43+
#[warn(clippy::std_instead_of_alloc)]
44+
fn std_instead_of_alloc() {
45+
// Only lint once.
46+
use alloc::vec;
47+
//~^ ERROR: used import from `std` instead of `alloc`
48+
use alloc::vec::Vec;
49+
//~^ ERROR: used import from `std` instead of `alloc`
50+
}
51+
52+
#[warn(clippy::alloc_instead_of_core)]
53+
fn alloc_instead_of_core() {
54+
use core::slice::from_ref;
55+
//~^ ERROR: used import from `alloc` instead of `core`
56+
}
57+
58+
fn main() {
59+
std_instead_of_core();
60+
std_instead_of_alloc();
61+
alloc_instead_of_core();
62+
}

tests/ui/std_instead_of_core.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ fn std_instead_of_core() {
1717
// Multiple imports
1818
use std::fmt::{Debug, Result};
1919
//~^ ERROR: used import from `std` instead of `core`
20-
//~| ERROR: used import from `std` instead of `core`
2120

2221
// Function calls
2322
let ptr = std::ptr::null::<u32>();

tests/ui/std_instead_of_core.stderr

+22-49
Original file line numberDiff line numberDiff line change
@@ -2,103 +2,76 @@ error: used import from `std` instead of `core`
22
--> $DIR/std_instead_of_core.rs:9:9
33
|
44
LL | use std::hash::Hasher;
5-
| ^^^^^^^^^^^^^^^^^
5+
| ^^^ help: consider importing the item from `core`: `core`
66
|
7-
= help: consider importing the item from `core`
87
= note: `-D clippy::std-instead-of-core` implied by `-D warnings`
98
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_core)]`
109

1110
error: used import from `std` instead of `core`
12-
--> $DIR/std_instead_of_core.rs:12:9
11+
--> $DIR/std_instead_of_core.rs:12:11
1312
|
1413
LL | use ::std::hash::Hash;
15-
| ^^^^^^^^^^^^^^^^^
16-
|
17-
= help: consider importing the item from `core`
14+
| ^^^ help: consider importing the item from `core`: `core`
1815

1916
error: used import from `std` instead of `core`
20-
--> $DIR/std_instead_of_core.rs:18:20
17+
--> $DIR/std_instead_of_core.rs:18:9
2118
|
2219
LL | use std::fmt::{Debug, Result};
23-
| ^^^^^
24-
|
25-
= help: consider importing the item from `core`
26-
27-
error: used import from `std` instead of `core`
28-
--> $DIR/std_instead_of_core.rs:18:27
29-
|
30-
LL | use std::fmt::{Debug, Result};
31-
| ^^^^^^
32-
|
33-
= help: consider importing the item from `core`
20+
| ^^^ help: consider importing the item from `core`: `core`
3421

3522
error: used import from `std` instead of `core`
36-
--> $DIR/std_instead_of_core.rs:23:15
23+
--> $DIR/std_instead_of_core.rs:22:15
3724
|
3825
LL | let ptr = std::ptr::null::<u32>();
39-
| ^^^^^^^^^^^^^^^^^^^^^
40-
|
41-
= help: consider importing the item from `core`
26+
| ^^^ help: consider importing the item from `core`: `core`
4227

4328
error: used import from `std` instead of `core`
44-
--> $DIR/std_instead_of_core.rs:25:19
29+
--> $DIR/std_instead_of_core.rs:24:21
4530
|
4631
LL | let ptr_mut = ::std::ptr::null_mut::<usize>();
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48-
|
49-
= help: consider importing the item from `core`
32+
| ^^^ help: consider importing the item from `core`: `core`
5033

5134
error: used import from `std` instead of `core`
52-
--> $DIR/std_instead_of_core.rs:29:16
35+
--> $DIR/std_instead_of_core.rs:28:16
5336
|
5437
LL | let cell = std::cell::Cell::new(8u32);
55-
| ^^^^^^^^^^^^^^^
56-
|
57-
= help: consider importing the item from `core`
38+
| ^^^ help: consider importing the item from `core`: `core`
5839

5940
error: used import from `std` instead of `core`
60-
--> $DIR/std_instead_of_core.rs:31:25
41+
--> $DIR/std_instead_of_core.rs:30:27
6142
|
6243
LL | let cell_absolute = ::std::cell::Cell::new(8u32);
63-
| ^^^^^^^^^^^^^^^^^
64-
|
65-
= help: consider importing the item from `core`
44+
| ^^^ help: consider importing the item from `core`: `core`
6645

6746
error: used import from `std` instead of `core`
68-
--> $DIR/std_instead_of_core.rs:40:9
47+
--> $DIR/std_instead_of_core.rs:39:9
6948
|
7049
LL | use std::iter::Iterator;
71-
| ^^^^^^^^^^^^^^^^^^^
72-
|
73-
= help: consider importing the item from `core`
50+
| ^^^ help: consider importing the item from `core`: `core`
7451

7552
error: used import from `std` instead of `alloc`
76-
--> $DIR/std_instead_of_core.rs:47:9
53+
--> $DIR/std_instead_of_core.rs:46:9
7754
|
7855
LL | use std::vec;
79-
| ^^^^^^^^
56+
| ^^^ help: consider importing the item from `alloc`: `alloc`
8057
|
81-
= help: consider importing the item from `alloc`
8258
= note: `-D clippy::std-instead-of-alloc` implied by `-D warnings`
8359
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_alloc)]`
8460

8561
error: used import from `std` instead of `alloc`
86-
--> $DIR/std_instead_of_core.rs:49:9
62+
--> $DIR/std_instead_of_core.rs:48:9
8763
|
8864
LL | use std::vec::Vec;
89-
| ^^^^^^^^^^^^^
90-
|
91-
= help: consider importing the item from `alloc`
65+
| ^^^ help: consider importing the item from `alloc`: `alloc`
9266

9367
error: used import from `alloc` instead of `core`
94-
--> $DIR/std_instead_of_core.rs:55:9
68+
--> $DIR/std_instead_of_core.rs:54:9
9569
|
9670
LL | use alloc::slice::from_ref;
97-
| ^^^^^^^^^^^^^^^^^^^^^^
71+
| ^^^^^ help: consider importing the item from `core`: `core`
9872
|
99-
= help: consider importing the item from `core`
10073
= note: `-D clippy::alloc-instead-of-core` implied by `-D warnings`
10174
= help: to override `-D warnings` add `#[allow(clippy::alloc_instead_of_core)]`
10275

103-
error: aborting due to 12 previous errors
76+
error: aborting due to 11 previous errors
10477

0 commit comments

Comments
 (0)