2021-03-25 18:29:11 +00:00
|
|
|
use crate::{map_unit_fn::OPTION_MAP_UNIT_FN, matches::MATCH_AS_REF};
|
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-09-08 14:31:47 +00:00
|
|
|
use clippy_utils::higher::IfLetOrMatch;
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
|
2021-11-13 00:12:17 +00:00
|
|
|
use clippy_utils::ty::{is_type_diagnostic_item, peel_mid_ty_refs_is_mutable, type_is_unsafe_function};
|
2021-04-22 09:31:13 +00:00
|
|
|
use clippy_utils::{
|
2021-07-15 08:44:10 +00:00
|
|
|
can_move_expr_to_closure, in_constant, is_else_clause, is_lang_ctor, is_lint_allowed, path_to_local_id,
|
2021-09-08 14:31:47 +00:00
|
|
|
peel_hir_expr_refs, peel_hir_expr_while, CaptureKind,
|
2021-04-22 09:31:13 +00:00
|
|
|
};
|
2021-02-25 10:25:22 +00:00
|
|
|
use rustc_ast::util::parser::PREC_POSTFIX;
|
|
|
|
use rustc_errors::Applicability;
|
2021-04-22 09:31:13 +00:00
|
|
|
use rustc_hir::LangItem::{OptionNone, OptionSome};
|
2021-09-08 14:31:47 +00:00
|
|
|
use rustc_hir::{
|
2021-11-13 00:12:17 +00:00
|
|
|
def::Res, Arm, BindingAnnotation, Block, BlockCheckMode, Expr, ExprKind, HirId, Mutability, Pat, PatKind, Path,
|
|
|
|
QPath, UnsafeSource,
|
2021-09-08 14:31:47 +00:00
|
|
|
};
|
2021-02-25 10:25:22 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
|
|
|
use rustc_middle::lint::in_external_macro;
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-07-01 16:17:38 +00:00
|
|
|
use rustc_span::{sym, SyntaxContext};
|
2021-02-25 10:25:22 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for usages of `match` which could be implemented using `map`
|
2021-02-25 10:25:22 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Using the `map` method is clearer and more concise.
|
2021-02-25 10:25:22 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2021-02-25 10:25:22 +00:00
|
|
|
/// ```rust
|
|
|
|
/// match Some(0) {
|
|
|
|
/// Some(x) => Some(x + 1),
|
|
|
|
/// None => None,
|
|
|
|
/// };
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// Some(0).map(|x| x + 1);
|
|
|
|
/// ```
|
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.52.0"]
|
2021-02-25 10:25:22 +00:00
|
|
|
pub MANUAL_MAP,
|
|
|
|
style,
|
|
|
|
"reimplementation of `map`"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(ManualMap => [MANUAL_MAP]);
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for ManualMap {
|
|
|
|
#[allow(clippy::too_many_lines)]
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2021-09-08 14:31:47 +00:00
|
|
|
let (scrutinee, then_pat, then_body, else_pat, else_body) = match IfLetOrMatch::parse(cx, expr) {
|
|
|
|
Some(IfLetOrMatch::IfLet(scrutinee, pat, body, Some(r#else))) => (scrutinee, pat, body, None, r#else),
|
|
|
|
Some(IfLetOrMatch::Match(
|
|
|
|
scrutinee,
|
|
|
|
[arm1 @ Arm { guard: None, .. }, arm2 @ Arm { guard: None, .. }],
|
|
|
|
_,
|
|
|
|
)) => (scrutinee, arm1.pat, arm1.body, Some(arm2.pat), arm2.body),
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
if in_external_macro(cx.sess(), expr.span) || in_constant(cx, expr.hir_id) {
|
|
|
|
return;
|
2021-08-08 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
let (scrutinee_ty, ty_ref_count, ty_mutability) =
|
|
|
|
peel_mid_ty_refs_is_mutable(cx.typeck_results().expr_ty(scrutinee));
|
2021-10-02 23:51:01 +00:00
|
|
|
if !(is_type_diagnostic_item(cx, scrutinee_ty, sym::Option)
|
|
|
|
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::Option))
|
2021-08-08 14:49:13 +00:00
|
|
|
{
|
2021-09-08 14:31:47 +00:00
|
|
|
return;
|
2021-08-08 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
let expr_ctxt = expr.span.ctxt();
|
|
|
|
let (some_expr, some_pat, pat_ref_count, is_wild_none) = match (
|
|
|
|
try_parse_pattern(cx, then_pat, expr_ctxt),
|
|
|
|
else_pat.map_or(Some(OptionPat::Wild), |p| try_parse_pattern(cx, p, expr_ctxt)),
|
|
|
|
) {
|
|
|
|
(Some(OptionPat::Wild), Some(OptionPat::Some { pattern, ref_count })) if is_none_expr(cx, then_body) => {
|
|
|
|
(else_body, pattern, ref_count, true)
|
|
|
|
},
|
|
|
|
(Some(OptionPat::None), Some(OptionPat::Some { pattern, ref_count })) if is_none_expr(cx, then_body) => {
|
|
|
|
(else_body, pattern, ref_count, false)
|
|
|
|
},
|
|
|
|
(Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::Wild)) if is_none_expr(cx, else_body) => {
|
|
|
|
(then_body, pattern, ref_count, true)
|
|
|
|
},
|
|
|
|
(Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::None)) if is_none_expr(cx, else_body) => {
|
|
|
|
(then_body, pattern, ref_count, false)
|
|
|
|
},
|
|
|
|
_ => return,
|
|
|
|
};
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
// Top level or patterns aren't allowed in closures.
|
|
|
|
if matches!(some_pat.kind, PatKind::Or(_)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-11-13 00:12:17 +00:00
|
|
|
let some_expr = match get_some_expr(cx, some_expr, false, expr_ctxt) {
|
2021-09-08 14:31:47 +00:00
|
|
|
Some(expr) => expr,
|
|
|
|
None => return,
|
|
|
|
};
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
// These two lints will go back and forth with each other.
|
2021-11-13 00:12:17 +00:00
|
|
|
if cx.typeck_results().expr_ty(some_expr.expr) == cx.tcx.types.unit
|
2021-09-08 14:31:47 +00:00
|
|
|
&& !is_lint_allowed(cx, OPTION_MAP_UNIT_FN, expr.hir_id)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
// `map` won't perform any adjustments.
|
2021-11-13 00:12:17 +00:00
|
|
|
if !cx.typeck_results().expr_adjustments(some_expr.expr).is_empty() {
|
2021-09-08 14:31:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
// Determine which binding mode to use.
|
|
|
|
let explicit_ref = some_pat.contains_explicit_ref_binding();
|
|
|
|
let binding_ref = explicit_ref.or_else(|| (ty_ref_count != pat_ref_count).then(|| ty_mutability));
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
let as_ref_str = match binding_ref {
|
|
|
|
Some(Mutability::Mut) => ".as_mut()",
|
|
|
|
Some(Mutability::Not) => ".as_ref()",
|
|
|
|
None => "",
|
|
|
|
};
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-11-13 00:12:17 +00:00
|
|
|
match can_move_expr_to_closure(cx, some_expr.expr) {
|
2021-09-08 14:31:47 +00:00
|
|
|
Some(captures) => {
|
|
|
|
// Check if captures the closure will need conflict with borrows made in the scrutinee.
|
|
|
|
// TODO: check all the references made in the scrutinee expression. This will require interacting
|
|
|
|
// with the borrow checker. Currently only `<local>[.<field>]*` is checked for.
|
|
|
|
if let Some(binding_ref_mutability) = binding_ref {
|
|
|
|
let e = peel_hir_expr_while(scrutinee, |e| match e.kind {
|
|
|
|
ExprKind::Field(e, _) | ExprKind::AddrOf(_, _, e) => Some(e),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
if let ExprKind::Path(QPath::Resolved(None, Path { res: Res::Local(l), .. })) = e.kind {
|
|
|
|
match captures.get(l) {
|
|
|
|
Some(CaptureKind::Value | CaptureKind::Ref(Mutability::Mut)) => return,
|
|
|
|
Some(CaptureKind::Ref(Mutability::Not)) if binding_ref_mutability == Mutability::Mut => {
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
Some(CaptureKind::Ref(Mutability::Not)) | None => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => return,
|
|
|
|
};
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
let mut app = Applicability::MachineApplicable;
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
// Remove address-of expressions from the scrutinee. Either `as_ref` will be called, or
|
|
|
|
// it's being passed by value.
|
|
|
|
let scrutinee = peel_hir_expr_refs(scrutinee).0;
|
|
|
|
let (scrutinee_str, _) = snippet_with_context(cx, scrutinee.span, expr_ctxt, "..", &mut app);
|
|
|
|
let scrutinee_str =
|
|
|
|
if scrutinee.span.ctxt() == expr.span.ctxt() && scrutinee.precedence().order() < PREC_POSTFIX {
|
|
|
|
format!("({})", scrutinee_str)
|
|
|
|
} else {
|
|
|
|
scrutinee_str.into()
|
|
|
|
};
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
let body_str = if let PatKind::Binding(annotation, id, some_binding, None) = some_pat.kind {
|
2021-11-13 00:12:17 +00:00
|
|
|
if_chain! {
|
|
|
|
if !some_expr.needs_unsafe_block;
|
|
|
|
if let Some(func) = can_pass_as_func(cx, id, some_expr.expr);
|
|
|
|
if func.span.ctxt() == some_expr.expr.span.ctxt();
|
|
|
|
then {
|
2021-09-08 14:31:47 +00:00
|
|
|
snippet_with_applicability(cx, func.span, "..", &mut app).into_owned()
|
2021-11-13 00:12:17 +00:00
|
|
|
} else {
|
|
|
|
if path_to_local_id(some_expr.expr, id)
|
2021-09-08 14:31:47 +00:00
|
|
|
&& !is_lint_allowed(cx, MATCH_AS_REF, expr.hir_id)
|
|
|
|
&& binding_ref.is_some()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
// `ref` and `ref mut` annotations were handled earlier.
|
|
|
|
let annotation = if matches!(annotation, BindingAnnotation::Mutable) {
|
|
|
|
"mut "
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
2021-11-13 00:12:17 +00:00
|
|
|
if some_expr.needs_unsafe_block {
|
|
|
|
format!(
|
|
|
|
"|{}{}| unsafe {{ {} }}",
|
|
|
|
annotation,
|
|
|
|
some_binding,
|
|
|
|
snippet_with_context(cx, some_expr.expr.span, expr_ctxt, "..", &mut app).0
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"|{}{}| {}",
|
|
|
|
annotation,
|
|
|
|
some_binding,
|
|
|
|
snippet_with_context(cx, some_expr.expr.span, expr_ctxt, "..", &mut app).0
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 14:31:47 +00:00
|
|
|
}
|
|
|
|
} else if !is_wild_none && explicit_ref.is_none() {
|
|
|
|
// TODO: handle explicit reference annotations.
|
2021-11-13 00:12:17 +00:00
|
|
|
if some_expr.needs_unsafe_block {
|
|
|
|
format!(
|
|
|
|
"|{}| unsafe {{ {} }}",
|
|
|
|
snippet_with_context(cx, some_pat.span, expr_ctxt, "..", &mut app).0,
|
|
|
|
snippet_with_context(cx, some_expr.expr.span, expr_ctxt, "..", &mut app).0
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"|{}| {}",
|
|
|
|
snippet_with_context(cx, some_pat.span, expr_ctxt, "..", &mut app).0,
|
|
|
|
snippet_with_context(cx, some_expr.expr.span, expr_ctxt, "..", &mut app).0
|
|
|
|
)
|
|
|
|
}
|
2021-09-08 14:31:47 +00:00
|
|
|
} else {
|
|
|
|
// Refutable bindings and mixed reference annotations can't be handled by `map`.
|
|
|
|
return;
|
|
|
|
};
|
2021-08-08 14:49:13 +00:00
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MANUAL_MAP,
|
|
|
|
expr.span,
|
|
|
|
"manual implementation of `Option::map`",
|
|
|
|
"try this",
|
|
|
|
if else_pat.is_none() && is_else_clause(cx.tcx, expr) {
|
|
|
|
format!("{{ {}{}.map({}) }}", scrutinee_str, as_ref_str, body_str)
|
|
|
|
} else {
|
|
|
|
format!("{}{}.map({})", scrutinee_str, as_ref_str, body_str)
|
2021-08-08 14:49:13 +00:00
|
|
|
},
|
2021-09-08 14:31:47 +00:00
|
|
|
app,
|
|
|
|
);
|
|
|
|
}
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether the expression could be passed as a function, or whether a closure is needed.
|
|
|
|
// Returns the function to be passed to `map` if it exists.
|
2021-07-01 16:17:38 +00:00
|
|
|
fn can_pass_as_func(cx: &LateContext<'tcx>, binding: HirId, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
|
2021-02-25 10:25:22 +00:00
|
|
|
match expr.kind {
|
|
|
|
ExprKind::Call(func, [arg])
|
2021-11-13 00:12:17 +00:00
|
|
|
if path_to_local_id(arg, binding)
|
|
|
|
&& cx.typeck_results().expr_adjustments(arg).is_empty()
|
|
|
|
&& !type_is_unsafe_function(cx, cx.typeck_results().expr_ty(func).peel_refs()) =>
|
2021-02-25 10:25:22 +00:00
|
|
|
{
|
|
|
|
Some(func)
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum OptionPat<'a> {
|
|
|
|
Wild,
|
|
|
|
None,
|
|
|
|
Some {
|
|
|
|
// The pattern contained in the `Some` tuple.
|
|
|
|
pattern: &'a Pat<'a>,
|
|
|
|
// The number of references before the `Some` tuple.
|
|
|
|
// e.g. `&&Some(_)` has a ref count of 2.
|
|
|
|
ref_count: usize,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-13 00:12:17 +00:00
|
|
|
struct SomeExpr<'tcx> {
|
|
|
|
expr: &'tcx Expr<'tcx>,
|
|
|
|
needs_unsafe_block: bool,
|
|
|
|
}
|
|
|
|
|
2021-02-25 10:25:22 +00:00
|
|
|
// Try to parse into a recognized `Option` pattern.
|
|
|
|
// i.e. `_`, `None`, `Some(..)`, or a reference to any of those.
|
2021-09-08 14:31:47 +00:00
|
|
|
fn try_parse_pattern(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, ctxt: SyntaxContext) -> Option<OptionPat<'tcx>> {
|
|
|
|
fn f(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, ref_count: usize, ctxt: SyntaxContext) -> Option<OptionPat<'tcx>> {
|
|
|
|
match pat.kind {
|
2021-02-25 10:25:22 +00:00
|
|
|
PatKind::Wild => Some(OptionPat::Wild),
|
2021-09-08 14:31:47 +00:00
|
|
|
PatKind::Ref(pat, _) => f(cx, pat, ref_count + 1, ctxt),
|
2021-04-22 09:31:13 +00:00
|
|
|
PatKind::Path(ref qpath) if is_lang_ctor(cx, qpath, OptionNone) => Some(OptionPat::None),
|
2021-09-08 14:31:47 +00:00
|
|
|
PatKind::TupleStruct(ref qpath, [pattern], _)
|
|
|
|
if is_lang_ctor(cx, qpath, OptionSome) && pat.span.ctxt() == ctxt =>
|
|
|
|
{
|
2021-02-25 10:25:22 +00:00
|
|
|
Some(OptionPat::Some { pattern, ref_count })
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 14:31:47 +00:00
|
|
|
f(cx, pat, 0, ctxt)
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks for an expression wrapped by the `Some` constructor. Returns the contained expression.
|
2021-11-13 00:12:17 +00:00
|
|
|
fn get_some_expr(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
expr: &'tcx Expr<'_>,
|
|
|
|
needs_unsafe_block: bool,
|
|
|
|
ctxt: SyntaxContext,
|
|
|
|
) -> Option<SomeExpr<'tcx>> {
|
2021-02-25 10:25:22 +00:00
|
|
|
// TODO: Allow more complex expressions.
|
|
|
|
match expr.kind {
|
|
|
|
ExprKind::Call(
|
|
|
|
Expr {
|
2021-04-22 09:31:13 +00:00
|
|
|
kind: ExprKind::Path(ref qpath),
|
2021-02-25 10:25:22 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
[arg],
|
2021-11-13 00:12:17 +00:00
|
|
|
) if ctxt == expr.span.ctxt() && is_lang_ctor(cx, qpath, OptionSome) => Some(SomeExpr {
|
|
|
|
expr: arg,
|
|
|
|
needs_unsafe_block,
|
|
|
|
}),
|
2021-02-25 10:25:22 +00:00
|
|
|
ExprKind::Block(
|
|
|
|
Block {
|
|
|
|
stmts: [],
|
|
|
|
expr: Some(expr),
|
2021-11-13 00:12:17 +00:00
|
|
|
rules,
|
2021-02-25 10:25:22 +00:00
|
|
|
..
|
|
|
|
},
|
|
|
|
_,
|
2021-11-13 00:12:17 +00:00
|
|
|
) => get_some_expr(
|
|
|
|
cx,
|
|
|
|
expr,
|
|
|
|
needs_unsafe_block || *rules == BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided),
|
|
|
|
ctxt,
|
|
|
|
),
|
2021-02-25 10:25:22 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks for the `None` value.
|
|
|
|
fn is_none_expr(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
|
|
|
|
match expr.kind {
|
2021-04-22 09:31:13 +00:00
|
|
|
ExprKind::Path(ref qpath) => is_lang_ctor(cx, qpath, OptionNone),
|
2021-02-25 10:25:22 +00:00
|
|
|
ExprKind::Block(
|
|
|
|
Block {
|
|
|
|
stmts: [],
|
|
|
|
expr: Some(expr),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
_,
|
|
|
|
) => is_none_expr(cx, expr),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|