2021-09-28 17:03:12 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::fn_def_id;
|
2020-10-09 10:45:29 +00:00
|
|
|
|
2021-09-12 09:58:27 +00:00
|
|
|
use rustc_hir::{def::Res, def_id::DefIdMap, Expr};
|
2020-10-09 10:45:29 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-09-28 17:03:12 +00:00
|
|
|
|
|
|
|
use crate::utils::conf;
|
2020-10-09 10:45:29 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Denies the configured methods and functions in clippy.toml
|
2020-10-09 10:45:29 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
2021-09-28 17:03:12 +00:00
|
|
|
/// Some methods are undesirable in certain contexts, and it's beneficial to
|
|
|
|
/// lint for them as needed.
|
2020-10-09 10:45:29 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2021-02-11 14:04:38 +00:00
|
|
|
/// An example clippy.toml configuration:
|
|
|
|
/// ```toml
|
|
|
|
/// # clippy.toml
|
2021-09-28 17:03:12 +00:00
|
|
|
/// disallowed-methods = [
|
|
|
|
/// # Can use a string as the path of the disallowed method.
|
|
|
|
/// "std::boxed::Box::new",
|
|
|
|
/// # Can also use an inline table with a `path` key.
|
|
|
|
/// { path = "std::time::Instant::now" },
|
|
|
|
/// # When using an inline table, can add a `reason` for why the method
|
|
|
|
/// # is disallowed.
|
|
|
|
/// { path = "std::vec::Vec::leak", reason = "no leaking memory" },
|
|
|
|
/// ]
|
2021-02-11 14:04:38 +00:00
|
|
|
/// ```
|
|
|
|
///
|
2020-10-09 10:45:29 +00:00
|
|
|
/// ```rust,ignore
|
2021-02-11 14:04:38 +00:00
|
|
|
/// // Example code where clippy issues a warning
|
|
|
|
/// let xs = vec![1, 2, 3, 4];
|
|
|
|
/// xs.leak(); // Vec::leak is disallowed in the config.
|
2021-09-28 17:03:12 +00:00
|
|
|
/// // The diagnostic contains the message "no leaking memory".
|
2021-02-11 14:04:38 +00:00
|
|
|
///
|
|
|
|
/// let _now = Instant::now(); // Instant::now is disallowed in the config.
|
2021-09-28 17:03:12 +00:00
|
|
|
///
|
|
|
|
/// let _box = Box::new(3); // Box::new is disallowed in the config.
|
2020-10-09 10:45:29 +00:00
|
|
|
/// ```
|
2021-02-11 14:04:38 +00:00
|
|
|
///
|
2020-10-09 10:45:29 +00:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust,ignore
|
2021-02-11 14:04:38 +00:00
|
|
|
/// // Example code which does not raise clippy warning
|
|
|
|
/// let mut xs = Vec::new(); // Vec::new is _not_ disallowed in the config.
|
|
|
|
/// xs.push(123); // Vec::push is _not_ disallowed in the config.
|
2020-10-09 10:45:29 +00:00
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 19:06:26 +00:00
|
|
|
#[clippy::version = "1.49.0"]
|
2020-10-09 10:45:29 +00:00
|
|
|
pub DISALLOWED_METHOD,
|
|
|
|
nursery,
|
|
|
|
"use of a disallowed method call"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct DisallowedMethod {
|
2021-09-28 17:03:12 +00:00
|
|
|
conf_disallowed: Vec<conf::DisallowedMethod>,
|
|
|
|
disallowed: DefIdMap<Option<String>>,
|
2020-10-09 10:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DisallowedMethod {
|
2021-09-28 17:03:12 +00:00
|
|
|
pub fn new(conf_disallowed: Vec<conf::DisallowedMethod>) -> Self {
|
2020-10-09 10:45:29 +00:00
|
|
|
Self {
|
2021-09-28 17:03:12 +00:00
|
|
|
conf_disallowed,
|
|
|
|
disallowed: DefIdMap::default(),
|
2020-10-09 10:45:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for DisallowedMethod {
|
2021-09-12 09:58:27 +00:00
|
|
|
fn check_crate(&mut self, cx: &LateContext<'_>) {
|
2021-09-28 17:03:12 +00:00
|
|
|
for conf in &self.conf_disallowed {
|
|
|
|
let (path, reason) = match conf {
|
|
|
|
conf::DisallowedMethod::Simple(path) => (path, None),
|
|
|
|
conf::DisallowedMethod::WithReason { path, reason } => (
|
|
|
|
path,
|
|
|
|
reason.as_ref().map(|reason| format!("{} (from clippy.toml)", reason)),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
let segs: Vec<_> = path.split("::").collect();
|
|
|
|
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &segs) {
|
|
|
|
self.disallowed.insert(id, reason);
|
2021-07-01 16:17:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:45:29 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2021-09-28 17:03:12 +00:00
|
|
|
let def_id = match fn_def_id(cx, expr) {
|
|
|
|
Some(def_id) => def_id,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
let reason = match self.disallowed.get(&def_id) {
|
|
|
|
Some(reason) => reason,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
let func_path = cx.tcx.def_path_str(def_id);
|
|
|
|
let msg = format!("use of a disallowed method `{}`", func_path);
|
|
|
|
span_lint_and_then(cx, DISALLOWED_METHOD, expr.span, &msg, |diag| {
|
|
|
|
if let Some(reason) = reason {
|
|
|
|
diag.note(reason);
|
2020-10-09 10:45:29 +00:00
|
|
|
}
|
2021-09-28 17:03:12 +00:00
|
|
|
});
|
2020-10-09 10:45:29 +00:00
|
|
|
}
|
|
|
|
}
|