Auto merge of #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 #11446
This commit is contained in:
commit
27165acadf
@ -1,4 +1,5 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_help;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::def::Res;
|
use rustc_hir::def::Res;
|
||||||
use rustc_hir::def_id::DefId;
|
use rustc_hir::def_id::DefId;
|
||||||
use rustc_hir::{HirId, Path, PathSegment};
|
use rustc_hir::{HirId, Path, PathSegment};
|
||||||
@ -99,17 +100,17 @@ fn check_path(&mut self, cx: &LateContext<'tcx>, path: &Path<'tcx>, _: HirId) {
|
|||||||
&& let Some(first_segment) = get_first_segment(path)
|
&& let Some(first_segment) = get_first_segment(path)
|
||||||
&& is_stable(cx, def_id)
|
&& is_stable(cx, def_id)
|
||||||
{
|
{
|
||||||
let (lint, msg, help) = match first_segment.ident.name {
|
let (lint, used_mod, replace_with) = match first_segment.ident.name {
|
||||||
sym::std => match cx.tcx.crate_name(def_id.krate) {
|
sym::std => match cx.tcx.crate_name(def_id.krate) {
|
||||||
sym::core => (
|
sym::core => (
|
||||||
STD_INSTEAD_OF_CORE,
|
STD_INSTEAD_OF_CORE,
|
||||||
"used import from `std` instead of `core`",
|
"std",
|
||||||
"consider importing the item from `core`",
|
"core",
|
||||||
),
|
),
|
||||||
sym::alloc => (
|
sym::alloc => (
|
||||||
STD_INSTEAD_OF_ALLOC,
|
STD_INSTEAD_OF_ALLOC,
|
||||||
"used import from `std` instead of `alloc`",
|
"std",
|
||||||
"consider importing the item from `alloc`",
|
"alloc",
|
||||||
),
|
),
|
||||||
_ => {
|
_ => {
|
||||||
self.prev_span = path.span;
|
self.prev_span = path.span;
|
||||||
@ -120,8 +121,8 @@ fn check_path(&mut self, cx: &LateContext<'tcx>, path: &Path<'tcx>, _: HirId) {
|
|||||||
if cx.tcx.crate_name(def_id.krate) == sym::core {
|
if cx.tcx.crate_name(def_id.krate) == sym::core {
|
||||||
(
|
(
|
||||||
ALLOC_INSTEAD_OF_CORE,
|
ALLOC_INSTEAD_OF_CORE,
|
||||||
"used import from `alloc` instead of `core`",
|
"alloc",
|
||||||
"consider importing the item from `core`",
|
"core",
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
self.prev_span = path.span;
|
self.prev_span = path.span;
|
||||||
@ -131,7 +132,14 @@ fn check_path(&mut self, cx: &LateContext<'tcx>, path: &Path<'tcx>, _: HirId) {
|
|||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
if path.span != self.prev_span {
|
if path.span != self.prev_span {
|
||||||
span_lint_and_help(cx, lint, path.span, msg, None, help);
|
span_lint_and_sugg(
|
||||||
|
cx,
|
||||||
|
lint,
|
||||||
|
first_segment.ident.span,
|
||||||
|
&format!("used import from `{used_mod}` instead of `{replace_with}`"),
|
||||||
|
&format!("consider importing the item from `{replace_with}`"),
|
||||||
|
replace_with.to_string(),
|
||||||
|
Applicability::MachineApplicable);
|
||||||
self.prev_span = path.span;
|
self.prev_span = path.span;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
62
tests/ui/std_instead_of_core.fixed
Normal file
62
tests/ui/std_instead_of_core.fixed
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
#![warn(clippy::std_instead_of_core)]
|
||||||
|
#![allow(unused_imports)]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
|
#[warn(clippy::std_instead_of_core)]
|
||||||
|
fn std_instead_of_core() {
|
||||||
|
// Regular import
|
||||||
|
use core::hash::Hasher;
|
||||||
|
//~^ ERROR: used import from `std` instead of `core`
|
||||||
|
// Absolute path
|
||||||
|
use ::core::hash::Hash;
|
||||||
|
//~^ ERROR: used import from `std` instead of `core`
|
||||||
|
// Don't lint on `env` macro
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
// Multiple imports
|
||||||
|
use core::fmt::{Debug, Result};
|
||||||
|
//~^ ERROR: used import from `std` instead of `core`
|
||||||
|
|
||||||
|
// Function calls
|
||||||
|
let ptr = core::ptr::null::<u32>();
|
||||||
|
//~^ ERROR: used import from `std` instead of `core`
|
||||||
|
let ptr_mut = ::core::ptr::null_mut::<usize>();
|
||||||
|
//~^ ERROR: used import from `std` instead of `core`
|
||||||
|
|
||||||
|
// Types
|
||||||
|
let cell = core::cell::Cell::new(8u32);
|
||||||
|
//~^ ERROR: used import from `std` instead of `core`
|
||||||
|
let cell_absolute = ::core::cell::Cell::new(8u32);
|
||||||
|
//~^ ERROR: used import from `std` instead of `core`
|
||||||
|
|
||||||
|
let _ = std::env!("PATH");
|
||||||
|
|
||||||
|
// do not lint until `error_in_core` is stable
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
// lint items re-exported from private modules, `core::iter::traits::iterator::Iterator`
|
||||||
|
use core::iter::Iterator;
|
||||||
|
//~^ ERROR: used import from `std` instead of `core`
|
||||||
|
}
|
||||||
|
|
||||||
|
#[warn(clippy::std_instead_of_alloc)]
|
||||||
|
fn std_instead_of_alloc() {
|
||||||
|
// Only lint once.
|
||||||
|
use alloc::vec;
|
||||||
|
//~^ ERROR: used import from `std` instead of `alloc`
|
||||||
|
use alloc::vec::Vec;
|
||||||
|
//~^ ERROR: used import from `std` instead of `alloc`
|
||||||
|
}
|
||||||
|
|
||||||
|
#[warn(clippy::alloc_instead_of_core)]
|
||||||
|
fn alloc_instead_of_core() {
|
||||||
|
use core::slice::from_ref;
|
||||||
|
//~^ ERROR: used import from `alloc` instead of `core`
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
std_instead_of_core();
|
||||||
|
std_instead_of_alloc();
|
||||||
|
alloc_instead_of_core();
|
||||||
|
}
|
@ -17,7 +17,6 @@ fn std_instead_of_core() {
|
|||||||
// Multiple imports
|
// Multiple imports
|
||||||
use std::fmt::{Debug, Result};
|
use std::fmt::{Debug, Result};
|
||||||
//~^ ERROR: used import from `std` instead of `core`
|
//~^ ERROR: used import from `std` instead of `core`
|
||||||
//~| ERROR: used import from `std` instead of `core`
|
|
||||||
|
|
||||||
// Function calls
|
// Function calls
|
||||||
let ptr = std::ptr::null::<u32>();
|
let ptr = std::ptr::null::<u32>();
|
||||||
|
@ -2,103 +2,76 @@ error: used import from `std` instead of `core`
|
|||||||
--> $DIR/std_instead_of_core.rs:9:9
|
--> $DIR/std_instead_of_core.rs:9:9
|
||||||
|
|
|
|
||||||
LL | use std::hash::Hasher;
|
LL | use std::hash::Hasher;
|
||||||
| ^^^^^^^^^^^^^^^^^
|
| ^^^ help: consider importing the item from `core`: `core`
|
||||||
|
|
|
|
||||||
= help: consider importing the item from `core`
|
|
||||||
= note: `-D clippy::std-instead-of-core` implied by `-D warnings`
|
= note: `-D clippy::std-instead-of-core` implied by `-D warnings`
|
||||||
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_core)]`
|
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_core)]`
|
||||||
|
|
||||||
error: used import from `std` instead of `core`
|
error: used import from `std` instead of `core`
|
||||||
--> $DIR/std_instead_of_core.rs:12:9
|
--> $DIR/std_instead_of_core.rs:12:11
|
||||||
|
|
|
|
||||||
LL | use ::std::hash::Hash;
|
LL | use ::std::hash::Hash;
|
||||||
| ^^^^^^^^^^^^^^^^^
|
| ^^^ help: consider importing the item from `core`: `core`
|
||||||
|
|
|
||||||
= help: consider importing the item from `core`
|
|
||||||
|
|
||||||
error: used import from `std` instead of `core`
|
error: used import from `std` instead of `core`
|
||||||
--> $DIR/std_instead_of_core.rs:18:20
|
--> $DIR/std_instead_of_core.rs:18:9
|
||||||
|
|
|
|
||||||
LL | use std::fmt::{Debug, Result};
|
LL | use std::fmt::{Debug, Result};
|
||||||
| ^^^^^
|
| ^^^ help: consider importing the item from `core`: `core`
|
||||||
|
|
|
||||||
= help: consider importing the item from `core`
|
|
||||||
|
|
||||||
error: used import from `std` instead of `core`
|
error: used import from `std` instead of `core`
|
||||||
--> $DIR/std_instead_of_core.rs:18:27
|
--> $DIR/std_instead_of_core.rs:22:15
|
||||||
|
|
|
||||||
LL | use std::fmt::{Debug, Result};
|
|
||||||
| ^^^^^^
|
|
||||||
|
|
|
||||||
= help: consider importing the item from `core`
|
|
||||||
|
|
||||||
error: used import from `std` instead of `core`
|
|
||||||
--> $DIR/std_instead_of_core.rs:23:15
|
|
||||||
|
|
|
|
||||||
LL | let ptr = std::ptr::null::<u32>();
|
LL | let ptr = std::ptr::null::<u32>();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^
|
| ^^^ help: consider importing the item from `core`: `core`
|
||||||
|
|
|
||||||
= help: consider importing the item from `core`
|
|
||||||
|
|
||||||
error: used import from `std` instead of `core`
|
error: used import from `std` instead of `core`
|
||||||
--> $DIR/std_instead_of_core.rs:25:19
|
--> $DIR/std_instead_of_core.rs:24:21
|
||||||
|
|
|
|
||||||
LL | let ptr_mut = ::std::ptr::null_mut::<usize>();
|
LL | let ptr_mut = ::std::ptr::null_mut::<usize>();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^ help: consider importing the item from `core`: `core`
|
||||||
|
|
|
||||||
= help: consider importing the item from `core`
|
|
||||||
|
|
||||||
error: used import from `std` instead of `core`
|
error: used import from `std` instead of `core`
|
||||||
--> $DIR/std_instead_of_core.rs:29:16
|
--> $DIR/std_instead_of_core.rs:28:16
|
||||||
|
|
|
|
||||||
LL | let cell = std::cell::Cell::new(8u32);
|
LL | let cell = std::cell::Cell::new(8u32);
|
||||||
| ^^^^^^^^^^^^^^^
|
| ^^^ help: consider importing the item from `core`: `core`
|
||||||
|
|
|
||||||
= help: consider importing the item from `core`
|
|
||||||
|
|
||||||
error: used import from `std` instead of `core`
|
error: used import from `std` instead of `core`
|
||||||
--> $DIR/std_instead_of_core.rs:31:25
|
--> $DIR/std_instead_of_core.rs:30:27
|
||||||
|
|
|
|
||||||
LL | let cell_absolute = ::std::cell::Cell::new(8u32);
|
LL | let cell_absolute = ::std::cell::Cell::new(8u32);
|
||||||
| ^^^^^^^^^^^^^^^^^
|
| ^^^ help: consider importing the item from `core`: `core`
|
||||||
|
|
|
||||||
= help: consider importing the item from `core`
|
|
||||||
|
|
||||||
error: used import from `std` instead of `core`
|
error: used import from `std` instead of `core`
|
||||||
--> $DIR/std_instead_of_core.rs:40:9
|
--> $DIR/std_instead_of_core.rs:39:9
|
||||||
|
|
|
|
||||||
LL | use std::iter::Iterator;
|
LL | use std::iter::Iterator;
|
||||||
| ^^^^^^^^^^^^^^^^^^^
|
| ^^^ help: consider importing the item from `core`: `core`
|
||||||
|
|
|
||||||
= help: consider importing the item from `core`
|
|
||||||
|
|
||||||
error: used import from `std` instead of `alloc`
|
error: used import from `std` instead of `alloc`
|
||||||
--> $DIR/std_instead_of_core.rs:47:9
|
--> $DIR/std_instead_of_core.rs:46:9
|
||||||
|
|
|
|
||||||
LL | use std::vec;
|
LL | use std::vec;
|
||||||
| ^^^^^^^^
|
| ^^^ help: consider importing the item from `alloc`: `alloc`
|
||||||
|
|
|
|
||||||
= help: consider importing the item from `alloc`
|
|
||||||
= note: `-D clippy::std-instead-of-alloc` implied by `-D warnings`
|
= note: `-D clippy::std-instead-of-alloc` implied by `-D warnings`
|
||||||
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_alloc)]`
|
= help: to override `-D warnings` add `#[allow(clippy::std_instead_of_alloc)]`
|
||||||
|
|
||||||
error: used import from `std` instead of `alloc`
|
error: used import from `std` instead of `alloc`
|
||||||
--> $DIR/std_instead_of_core.rs:49:9
|
--> $DIR/std_instead_of_core.rs:48:9
|
||||||
|
|
|
|
||||||
LL | use std::vec::Vec;
|
LL | use std::vec::Vec;
|
||||||
| ^^^^^^^^^^^^^
|
| ^^^ help: consider importing the item from `alloc`: `alloc`
|
||||||
|
|
|
||||||
= help: consider importing the item from `alloc`
|
|
||||||
|
|
||||||
error: used import from `alloc` instead of `core`
|
error: used import from `alloc` instead of `core`
|
||||||
--> $DIR/std_instead_of_core.rs:55:9
|
--> $DIR/std_instead_of_core.rs:54:9
|
||||||
|
|
|
|
||||||
LL | use alloc::slice::from_ref;
|
LL | use alloc::slice::from_ref;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^ help: consider importing the item from `core`: `core`
|
||||||
|
|
|
|
||||||
= help: consider importing the item from `core`
|
|
||||||
= note: `-D clippy::alloc-instead-of-core` implied by `-D warnings`
|
= note: `-D clippy::alloc-instead-of-core` implied by `-D warnings`
|
||||||
= help: to override `-D warnings` add `#[allow(clippy::alloc_instead_of_core)]`
|
= help: to override `-D warnings` add `#[allow(clippy::alloc_instead_of_core)]`
|
||||||
|
|
||||||
error: aborting due to 12 previous errors
|
error: aborting due to 11 previous errors
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user