-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
7 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
50 changes: 50 additions & 0 deletions
50
clippy_lints/src/utils/internal_lints/diagnostic_item_path.rs
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,50 @@ | ||
use clippy_utils::def_path_res; | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_ast::LitKind; | ||
use rustc_hir::{def::Res, Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for hardcoded paths to diagnostic items. | ||
/// | ||
/// ### Why is this bad? | ||
/// See: https://github.com/rust-lang/rust-clippy/issues/5393 | ||
#[clippy::version = "1.66.0"] | ||
pub DIAGNOSTIC_ITEM_PATH, | ||
internal, | ||
"hardcoded paths to diagnostic items" | ||
} | ||
|
||
declare_lint_pass!(DiagnosticItemPath => [DIAGNOSTIC_ITEM_PATH]); | ||
|
||
impl LateLintPass<'_> for DiagnosticItemPath { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if let ExprKind::Array(elements) = expr.kind | ||
&& let Some(path) = elements | ||
.iter() | ||
.map(|element| { | ||
if let ExprKind::Lit(lit) = &element.kind | ||
&& let LitKind::Str(s, _) = lit.node | ||
{ | ||
Some(s.as_str().to_owned()) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect::<Option<Vec<_>>>() | ||
&& let Res::Def(_, def_id) = def_path_res(cx, &path.iter().map(AsRef::as_ref).collect::<Vec<_>>()) | ||
&& let Some(sym) = cx.tcx.get_diagnostic_name(def_id) | ||
{ | ||
span_lint_and_help( | ||
cx, | ||
DIAGNOSTIC_ITEM_PATH, | ||
expr.span, | ||
"hardcoded path to a diagnostic item", | ||
None, | ||
&format!("rewrite the code to use `sym::{sym}`"), | ||
); | ||
} | ||
} | ||
} |
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,11 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::diagnostic_item_path)] | ||
|
||
fn main() { | ||
const DEREF_TRAIT: [&str; 4] = ["core", "ops", "deref", "Deref"]; | ||
|
||
const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", "deref"]; | ||
|
||
// Not yet a diagnostic item | ||
const DEREF_MUT_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "DerefMut", "deref_mut"]; | ||
} |
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,19 @@ | ||
error: hardcoded path to a diagnostic item | ||
--> $DIR/diagnostic_item_path.rs:5:36 | ||
| | ||
LL | const DEREF_TRAIT: [&str; 4] = ["core", "ops", "deref", "Deref"]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::diagnostic-item-path` implied by `-D warnings` | ||
= help: rewrite the code to use `sym::Deref` | ||
|
||
error: hardcoded path to a diagnostic item | ||
--> $DIR/diagnostic_item_path.rs:7:43 | ||
| | ||
LL | const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", "deref"]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: rewrite the code to use `sym::deref_method` | ||
|
||
error: aborting due to 2 previous errors | ||
|