-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_hir::{def::Res, Item, ItemKind}; | ||
use rustc_lint::{LateContext, LateLintPass, Lint}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::{sym, Symbol}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// // example code where clippy issues a warning | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// // example code which does not raise clippy warning | ||
/// ``` | ||
#[clippy::version = "1.64.0"] | ||
pub STD_INSTEAD_OF_CORE, | ||
nursery, | ||
"default lint description" | ||
} | ||
// TODO: Make multi pass: see DropForgetRef | ||
declare_lint_pass!(StdReexports => [STD_INSTEAD_OF_CORE]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for StdReexports { | ||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &Item<'tcx>) { | ||
// std_instead_of_core | ||
run_lint(cx, item, sym::core, sym::std, STD_INSTEAD_OF_CORE); | ||
// std_instead_of_alloc | ||
run_lint(cx, item, sym::alloc, sym::std, STD_INSTEAD_OF_CORE); | ||
// alloc_instead_of_core | ||
run_lint(cx, item, sym::core, sym::alloc, STD_INSTEAD_OF_CORE); | ||
} | ||
} | ||
|
||
fn run_lint( | ||
cx: &LateContext<'_>, | ||
item: &Item<'_>, | ||
item_crate_name: Symbol, | ||
use_crate_segment_name: Symbol, | ||
lint: &'static Lint, | ||
) { | ||
if_chain! { | ||
if let ItemKind::Use(path, _) = item.kind; | ||
if let Res::Def(_, def_id) = path.res; | ||
// check if the resolved path is in the crate | ||
if item_crate_name == cx.tcx.crate_name(def_id.krate); | ||
|
||
// check if the first segment of the path is from std or | ||
if let Some(path_root_segment) = path.segments.first(); | ||
|
||
// and check that the first segment of the import refers crate we lint for. | ||
if use_crate_segment_name == path_root_segment.ident.name; | ||
|
||
then { | ||
span_lint_and_help( | ||
cx, | ||
lint, | ||
path.span, | ||
&format!("used `{}` import instead of `{}`", use_crate_segment_name, item_crate_name), | ||
None, | ||
&format!("consider importing from `{}`", item_crate_name), | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![warn(clippy::std_instead_of_core)] | ||
#![allow(unused_imports)] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::slice::from_ref; | ||
use std::hash::Hasher; | ||
use std::vec::Vec; | ||
|
||
fn main() { | ||
let test1 = alloc::vec::Vec::<u32>::new(); | ||
let test = ::std::vec::Vec::<u32>::new(); | ||
} |