Auto merge of #6801 - Jarcho:manual_match_fix, r=phansch
Fix `manual_map` false positives fixes: #6795 fixes: #6797 fixes: #6811 fixes: #6819 changelog: Fix false positives for `manual_map` when `return`, `break`, `continue`, `yield`, `await`, and partially moved values are used. changelog: Don't expand macros in suggestions for `manual_map`
This commit is contained in:
commit
ece3543c9f
@ -1543,6 +1543,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||
LintId::of(&loops::WHILE_LET_ON_ITERATOR),
|
||||
LintId::of(&main_recursion::MAIN_RECURSION),
|
||||
LintId::of(&manual_async_fn::MANUAL_ASYNC_FN),
|
||||
LintId::of(&manual_map::MANUAL_MAP),
|
||||
LintId::of(&manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE),
|
||||
LintId::of(&manual_strip::MANUAL_STRIP),
|
||||
LintId::of(&manual_unwrap_or::MANUAL_UNWRAP_OR),
|
||||
@ -1774,6 +1775,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||
LintId::of(&loops::WHILE_LET_ON_ITERATOR),
|
||||
LintId::of(&main_recursion::MAIN_RECURSION),
|
||||
LintId::of(&manual_async_fn::MANUAL_ASYNC_FN),
|
||||
LintId::of(&manual_map::MANUAL_MAP),
|
||||
LintId::of(&manual_non_exhaustive::MANUAL_NON_EXHAUSTIVE),
|
||||
LintId::of(&map_clone::MAP_CLONE),
|
||||
LintId::of(&matches::INFALLIBLE_DESTRUCTURING_MATCH),
|
||||
@ -2050,7 +2052,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
||||
LintId::of(&floating_point_arithmetic::SUBOPTIMAL_FLOPS),
|
||||
LintId::of(&future_not_send::FUTURE_NOT_SEND),
|
||||
LintId::of(&let_if_seq::USELESS_LET_IF_SEQ),
|
||||
LintId::of(&manual_map::MANUAL_MAP),
|
||||
LintId::of(&missing_const_for_fn::MISSING_CONST_FOR_FN),
|
||||
LintId::of(&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL),
|
||||
LintId::of(&mutex_atomic::MUTEX_INTEGER),
|
||||
|
@ -2,24 +2,32 @@
|
||||
map_unit_fn::OPTION_MAP_UNIT_FN,
|
||||
matches::MATCH_AS_REF,
|
||||
utils::{
|
||||
is_allowed, is_type_diagnostic_item, match_def_path, match_var, paths, peel_hir_expr_refs,
|
||||
peel_mid_ty_refs_is_mutable, snippet_with_applicability, span_lint_and_sugg,
|
||||
can_partially_move_ty, is_allowed, is_type_diagnostic_item, match_def_path, match_var, paths,
|
||||
peel_hir_expr_refs, peel_mid_ty_refs_is_mutable, snippet_with_applicability, snippet_with_context,
|
||||
span_lint_and_sugg,
|
||||
},
|
||||
};
|
||||
use rustc_ast::util::parser::PREC_POSTFIX;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Arm, BindingAnnotation, Block, Expr, ExprKind, Mutability, Pat, PatKind, QPath};
|
||||
use rustc_hir::{
|
||||
def::Res,
|
||||
intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
|
||||
Arm, BindingAnnotation, Block, Expr, ExprKind, Mutability, Pat, PatKind, Path, QPath,
|
||||
};
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
use rustc_middle::lint::in_external_macro;
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::symbol::{sym, Ident};
|
||||
use rustc_span::{
|
||||
symbol::{sym, Ident},
|
||||
SyntaxContext,
|
||||
};
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** Checks for usages of `match` which could be implemented using `map`
|
||||
///
|
||||
/// **Why is this bad?** Using the `map` method is clearer and more concise.
|
||||
///
|
||||
/// **Known problems:** `map` is not capable of representing some control flow which works fine in `match`.
|
||||
/// **Known problems:** None.
|
||||
///
|
||||
/// **Example:**
|
||||
///
|
||||
@ -34,7 +42,7 @@
|
||||
/// Some(0).map(|x| x + 1);
|
||||
/// ```
|
||||
pub MANUAL_MAP,
|
||||
nursery,
|
||||
style,
|
||||
"reimplementation of `map`"
|
||||
}
|
||||
|
||||
@ -52,43 +60,46 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
{
|
||||
let (scrutinee_ty, ty_ref_count, ty_mutability) =
|
||||
peel_mid_ty_refs_is_mutable(cx.typeck_results().expr_ty(scrutinee));
|
||||
if !is_type_diagnostic_item(cx, scrutinee_ty, sym::option_type)
|
||||
|| !is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::option_type)
|
||||
if !(is_type_diagnostic_item(cx, scrutinee_ty, sym::option_type)
|
||||
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::option_type))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let (some_expr, some_pat, pat_ref_count, is_wild_none) =
|
||||
match (try_parse_pattern(cx, arm1.pat), try_parse_pattern(cx, arm2.pat)) {
|
||||
(Some(OptionPat::Wild), Some(OptionPat::Some { pattern, ref_count }))
|
||||
if is_none_expr(cx, arm1.body) =>
|
||||
{
|
||||
(arm2.body, pattern, ref_count, true)
|
||||
},
|
||||
(Some(OptionPat::None), Some(OptionPat::Some { pattern, ref_count }))
|
||||
if is_none_expr(cx, arm1.body) =>
|
||||
{
|
||||
(arm2.body, pattern, ref_count, false)
|
||||
},
|
||||
(Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::Wild))
|
||||
if is_none_expr(cx, arm2.body) =>
|
||||
{
|
||||
(arm1.body, pattern, ref_count, true)
|
||||
},
|
||||
(Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::None))
|
||||
if is_none_expr(cx, arm2.body) =>
|
||||
{
|
||||
(arm1.body, pattern, ref_count, false)
|
||||
},
|
||||
_ => return,
|
||||
};
|
||||
let expr_ctxt = expr.span.ctxt();
|
||||
let (some_expr, some_pat, pat_ref_count, is_wild_none) = match (
|
||||
try_parse_pattern(cx, arm1.pat, expr_ctxt),
|
||||
try_parse_pattern(cx, arm2.pat, expr_ctxt),
|
||||
) {
|
||||
(Some(OptionPat::Wild), Some(OptionPat::Some { pattern, ref_count }))
|
||||
if is_none_expr(cx, arm1.body) =>
|
||||
{
|
||||
(arm2.body, pattern, ref_count, true)
|
||||
},
|
||||
(Some(OptionPat::None), Some(OptionPat::Some { pattern, ref_count }))
|
||||
if is_none_expr(cx, arm1.body) =>
|
||||
{
|
||||
(arm2.body, pattern, ref_count, false)
|
||||
},
|
||||
(Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::Wild))
|
||||
if is_none_expr(cx, arm2.body) =>
|
||||
{
|
||||
(arm1.body, pattern, ref_count, true)
|
||||
},
|
||||
(Some(OptionPat::Some { pattern, ref_count }), Some(OptionPat::None))
|
||||
if is_none_expr(cx, arm2.body) =>
|
||||
{
|
||||
(arm1.body, pattern, ref_count, false)
|
||||
},
|
||||
_ => return,
|
||||
};
|
||||
|
||||
// Top level or patterns aren't allowed in closures.
|
||||
if matches!(some_pat.kind, PatKind::Or(_)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let some_expr = match get_some_expr(cx, some_expr) {
|
||||
let some_expr = match get_some_expr(cx, some_expr, expr_ctxt) {
|
||||
Some(expr) => expr,
|
||||
None => return,
|
||||
};
|
||||
@ -99,6 +110,10 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
return;
|
||||
}
|
||||
|
||||
if !can_move_expr_to_closure(cx, some_expr) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 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));
|
||||
@ -111,47 +126,50 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
|
||||
let mut app = Applicability::MachineApplicable;
|
||||
|
||||
// Remove address-of expressions from the scrutinee. `as_ref` will be called,
|
||||
// the type is copyable, or the option is being passed by value.
|
||||
// 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_applicability(cx, scrutinee.span, "_", &mut app);
|
||||
let scrutinee_str = if expr.precedence().order() < PREC_POSTFIX {
|
||||
// Parens are needed to chain method calls.
|
||||
format!("({})", scrutinee_str)
|
||||
} else {
|
||||
scrutinee_str.into()
|
||||
};
|
||||
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()
|
||||
};
|
||||
|
||||
let body_str = if let PatKind::Binding(annotation, _, some_binding, None) = some_pat.kind {
|
||||
if let Some(func) = can_pass_as_func(cx, some_binding, some_expr) {
|
||||
snippet_with_applicability(cx, func.span, "..", &mut app).into_owned()
|
||||
} else {
|
||||
if match_var(some_expr, some_binding.name)
|
||||
&& !is_allowed(cx, MATCH_AS_REF, expr.hir_id)
|
||||
&& binding_ref.is_some()
|
||||
{
|
||||
return;
|
||||
}
|
||||
match can_pass_as_func(cx, some_binding, some_expr) {
|
||||
Some(func) if func.span.ctxt() == some_expr.span.ctxt() => {
|
||||
snippet_with_applicability(cx, func.span, "..", &mut app).into_owned()
|
||||
},
|
||||
_ => {
|
||||
if match_var(some_expr, some_binding.name)
|
||||
&& !is_allowed(cx, MATCH_AS_REF, expr.hir_id)
|
||||
&& binding_ref.is_some()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// `ref` and `ref mut` annotations were handled earlier.
|
||||
let annotation = if matches!(annotation, BindingAnnotation::Mutable) {
|
||||
"mut "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
format!(
|
||||
"|{}{}| {}",
|
||||
annotation,
|
||||
some_binding,
|
||||
snippet_with_applicability(cx, some_expr.span, "..", &mut app)
|
||||
)
|
||||
// `ref` and `ref mut` annotations were handled earlier.
|
||||
let annotation = if matches!(annotation, BindingAnnotation::Mutable) {
|
||||
"mut "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
format!(
|
||||
"|{}{}| {}",
|
||||
annotation,
|
||||
some_binding,
|
||||
snippet_with_context(cx, some_expr.span, expr_ctxt, "..", &mut app)
|
||||
)
|
||||
},
|
||||
}
|
||||
} else if !is_wild_none && explicit_ref.is_none() {
|
||||
// TODO: handle explicit reference annotations.
|
||||
format!(
|
||||
"|{}| {}",
|
||||
snippet_with_applicability(cx, some_pat.span, "..", &mut app),
|
||||
snippet_with_applicability(cx, some_expr.span, "..", &mut app)
|
||||
snippet_with_context(cx, some_pat.span, expr_ctxt, "..", &mut app),
|
||||
snippet_with_context(cx, some_expr.span, expr_ctxt, "..", &mut app)
|
||||
)
|
||||
} else {
|
||||
// Refutable bindings and mixed reference annotations can't be handled by `map`.
|
||||
@ -171,6 +189,51 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
}
|
||||
}
|
||||
|
||||
// Checks if the expression can be moved into a closure as is.
|
||||
fn can_move_expr_to_closure(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
|
||||
struct V<'cx, 'tcx> {
|
||||
cx: &'cx LateContext<'tcx>,
|
||||
make_closure: bool,
|
||||
}
|
||||
impl Visitor<'tcx> for V<'_, 'tcx> {
|
||||
type Map = ErasedMap<'tcx>;
|
||||
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
||||
NestedVisitorMap::None
|
||||
}
|
||||
|
||||
fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
|
||||
match e.kind {
|
||||
ExprKind::Break(..)
|
||||
| ExprKind::Continue(_)
|
||||
| ExprKind::Ret(_)
|
||||
| ExprKind::Yield(..)
|
||||
| ExprKind::InlineAsm(_)
|
||||
| ExprKind::LlvmInlineAsm(_) => {
|
||||
self.make_closure = false;
|
||||
},
|
||||
// Accessing a field of a local value can only be done if the type isn't
|
||||
// partially moved.
|
||||
ExprKind::Field(base_expr, _)
|
||||
if matches!(
|
||||
base_expr.kind,
|
||||
ExprKind::Path(QPath::Resolved(_, Path { res: Res::Local(_), .. }))
|
||||
) && can_partially_move_ty(self.cx, self.cx.typeck_results().expr_ty(base_expr)) =>
|
||||
{
|
||||
// TODO: check if the local has been partially moved. Assume it has for now.
|
||||
self.make_closure = false;
|
||||
return;
|
||||
}
|
||||
_ => (),
|
||||
};
|
||||
walk_expr(self, e);
|
||||
}
|
||||
}
|
||||
|
||||
let mut v = V { cx, make_closure: true };
|
||||
v.visit_expr(expr);
|
||||
v.make_closure
|
||||
}
|
||||
|
||||
// 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.
|
||||
fn can_pass_as_func(cx: &LateContext<'tcx>, binding: Ident, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
|
||||
@ -198,11 +261,11 @@ enum OptionPat<'a> {
|
||||
|
||||
// Try to parse into a recognized `Option` pattern.
|
||||
// i.e. `_`, `None`, `Some(..)`, or a reference to any of those.
|
||||
fn try_parse_pattern(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) -> Option<OptionPat<'tcx>> {
|
||||
fn f(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, ref_count: usize) -> Option<OptionPat<'tcx>> {
|
||||
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 {
|
||||
PatKind::Wild => Some(OptionPat::Wild),
|
||||
PatKind::Ref(pat, _) => f(cx, pat, ref_count + 1),
|
||||
PatKind::Ref(pat, _) => f(cx, pat, ref_count + 1, ctxt),
|
||||
PatKind::Path(QPath::Resolved(None, path))
|
||||
if path
|
||||
.res
|
||||
@ -215,18 +278,19 @@ fn f(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, ref_count: usize) -> Option<Opt
|
||||
if path
|
||||
.res
|
||||
.opt_def_id()
|
||||
.map_or(false, |id| match_def_path(cx, id, &paths::OPTION_SOME)) =>
|
||||
.map_or(false, |id| match_def_path(cx, id, &paths::OPTION_SOME))
|
||||
&& pat.span.ctxt() == ctxt =>
|
||||
{
|
||||
Some(OptionPat::Some { pattern, ref_count })
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
f(cx, pat, 0)
|
||||
f(cx, pat, 0, ctxt)
|
||||
}
|
||||
|
||||
// Checks for an expression wrapped by the `Some` constructor. Returns the contained expression.
|
||||
fn get_some_expr(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
|
||||
fn get_some_expr(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, ctxt: SyntaxContext) -> Option<&'tcx Expr<'tcx>> {
|
||||
// TODO: Allow more complex expressions.
|
||||
match expr.kind {
|
||||
ExprKind::Call(
|
||||
@ -235,7 +299,7 @@ fn get_some_expr(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx E
|
||||
..
|
||||
},
|
||||
[arg],
|
||||
) => {
|
||||
) if ctxt == expr.span.ctxt() => {
|
||||
if match_def_path(cx, path.res.opt_def_id()?, &paths::OPTION_SOME) {
|
||||
Some(arg)
|
||||
} else {
|
||||
@ -249,7 +313,7 @@ fn get_some_expr(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx E
|
||||
..
|
||||
},
|
||||
_,
|
||||
) => get_some_expr(cx, expr),
|
||||
) => get_some_expr(cx, expr, ctxt),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
@ -73,11 +73,11 @@
|
||||
use rustc_middle::ty::{self, layout::IntegerExt, DefIdTree, Ty, TyCtxt, TypeFoldable};
|
||||
use rustc_semver::RustcVersion;
|
||||
use rustc_session::Session;
|
||||
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
||||
use rustc_span::hygiene::{self, ExpnKind, MacroKind};
|
||||
use rustc_span::source_map::original_sp;
|
||||
use rustc_span::sym;
|
||||
use rustc_span::symbol::{kw, Symbol};
|
||||
use rustc_span::{BytePos, Pos, Span, DUMMY_SP};
|
||||
use rustc_span::{BytePos, Pos, Span, SyntaxContext, DUMMY_SP};
|
||||
use rustc_target::abi::Integer;
|
||||
use rustc_trait_selection::traits::query::normalize::AtExt;
|
||||
use smallvec::SmallVec;
|
||||
@ -472,6 +472,18 @@ pub fn has_drop<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks whether a type can be partially moved.
|
||||
pub fn can_partially_move_ty(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
||||
if has_drop(cx, ty) || is_copy(cx, ty) {
|
||||
return false;
|
||||
}
|
||||
match ty.kind() {
|
||||
ty::Param(_) => false,
|
||||
ty::Adt(def, subs) => def.all_fields().any(|f| !is_copy(cx, f.ty(cx.tcx, subs))),
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the method names and argument list of nested method call expressions that make up
|
||||
/// `expr`. method/span lists are sorted with the most recent call first.
|
||||
pub fn method_calls<'tcx>(
|
||||
@ -762,6 +774,35 @@ pub fn snippet_block_with_applicability<'a, T: LintContext>(
|
||||
reindent_multiline(snip, true, indent)
|
||||
}
|
||||
|
||||
/// Same as `snippet_with_applicability`, but first walks the span up to the given context. This
|
||||
/// will result in the macro call, rather then the expansion, if the span is from a child context.
|
||||
/// If the span is not from a child context, it will be used directly instead.
|
||||
///
|
||||
/// e.g. Given the expression `&vec![]`, getting a snippet from the span for `vec![]` as a HIR node
|
||||
/// would result in `box []`. If given the context of the address of expression, this function will
|
||||
/// correctly get a snippet of `vec![]`.
|
||||
pub fn snippet_with_context(
|
||||
cx: &LateContext<'_>,
|
||||
span: Span,
|
||||
outer: SyntaxContext,
|
||||
default: &'a str,
|
||||
applicability: &mut Applicability,
|
||||
) -> Cow<'a, str> {
|
||||
let outer_span = hygiene::walk_chain(span, outer);
|
||||
let span = if outer_span.ctxt() == outer {
|
||||
outer_span
|
||||
} else {
|
||||
// The span is from a macro argument, and the outer context is the macro using the argument
|
||||
if *applicability != Applicability::Unspecified {
|
||||
*applicability = Applicability::MaybeIncorrect;
|
||||
}
|
||||
// TODO: get the argument span.
|
||||
span
|
||||
};
|
||||
|
||||
snippet_with_applicability(cx, span, default, applicability)
|
||||
}
|
||||
|
||||
/// Returns a new Span that extends the original Span to the first non-whitespace char of the first
|
||||
/// line.
|
||||
///
|
||||
|
@ -1,7 +1,14 @@
|
||||
// edition:2018
|
||||
// run-rustfix
|
||||
|
||||
#![warn(clippy::manual_map)]
|
||||
#![allow(clippy::no_effect, clippy::map_identity, clippy::unit_arg, clippy::match_ref_pats)]
|
||||
#![allow(
|
||||
clippy::no_effect,
|
||||
clippy::map_identity,
|
||||
clippy::unit_arg,
|
||||
clippy::match_ref_pats,
|
||||
dead_code
|
||||
)]
|
||||
|
||||
fn main() {
|
||||
Some(0).map(|_| 2);
|
||||
@ -67,4 +74,58 @@ fn main() {
|
||||
Some(Some((x, 1))) => Some(x),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
// #6795
|
||||
fn f1() -> Result<(), ()> {
|
||||
let _ = match Some(Ok(())) {
|
||||
Some(x) => Some(x?),
|
||||
None => None,
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
for &x in Some(Some(true)).iter() {
|
||||
let _ = match x {
|
||||
Some(x) => Some(if x { continue } else { x }),
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
|
||||
// #6797
|
||||
let x1 = (Some(String::new()), 0);
|
||||
let x2 = x1.0;
|
||||
match x2 {
|
||||
Some(x) => Some((x, x1.1)),
|
||||
None => None,
|
||||
};
|
||||
|
||||
struct S1 {
|
||||
x: Option<String>,
|
||||
y: u32,
|
||||
}
|
||||
impl S1 {
|
||||
fn f(self) -> Option<(String, u32)> {
|
||||
match self.x {
|
||||
Some(x) => Some((x, self.y)),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// #6811
|
||||
Some(0).map(|x| vec![x]);
|
||||
|
||||
option_env!("").map(String::from);
|
||||
|
||||
// #6819
|
||||
async fn f2(x: u32) -> u32 {
|
||||
x
|
||||
}
|
||||
|
||||
async fn f3() {
|
||||
match Some(0) {
|
||||
Some(x) => Some(f2(x).await),
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,14 @@
|
||||
// edition:2018
|
||||
// run-rustfix
|
||||
|
||||
#![warn(clippy::manual_map)]
|
||||
#![allow(clippy::no_effect, clippy::map_identity, clippy::unit_arg, clippy::match_ref_pats)]
|
||||
#![allow(
|
||||
clippy::no_effect,
|
||||
clippy::map_identity,
|
||||
clippy::unit_arg,
|
||||
clippy::match_ref_pats,
|
||||
dead_code
|
||||
)]
|
||||
|
||||
fn main() {
|
||||
match Some(0) {
|
||||
@ -119,4 +126,64 @@ fn main() {
|
||||
Some(Some((x, 1))) => Some(x),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
// #6795
|
||||
fn f1() -> Result<(), ()> {
|
||||
let _ = match Some(Ok(())) {
|
||||
Some(x) => Some(x?),
|
||||
None => None,
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
for &x in Some(Some(true)).iter() {
|
||||
let _ = match x {
|
||||
Some(x) => Some(if x { continue } else { x }),
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
|
||||
// #6797
|
||||
let x1 = (Some(String::new()), 0);
|
||||
let x2 = x1.0;
|
||||
match x2 {
|
||||
Some(x) => Some((x, x1.1)),
|
||||
None => None,
|
||||
};
|
||||
|
||||
struct S1 {
|
||||
x: Option<String>,
|
||||
y: u32,
|
||||
}
|
||||
impl S1 {
|
||||
fn f(self) -> Option<(String, u32)> {
|
||||
match self.x {
|
||||
Some(x) => Some((x, self.y)),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// #6811
|
||||
match Some(0) {
|
||||
Some(x) => Some(vec![x]),
|
||||
None => None,
|
||||
};
|
||||
|
||||
match option_env!("") {
|
||||
Some(x) => Some(String::from(x)),
|
||||
None => None,
|
||||
};
|
||||
|
||||
// #6819
|
||||
async fn f2(x: u32) -> u32 {
|
||||
x
|
||||
}
|
||||
|
||||
async fn f3() {
|
||||
match Some(0) {
|
||||
Some(x) => Some(f2(x).await),
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:7:5
|
||||
--> $DIR/manual_map_option.rs:14:5
|
||||
|
|
||||
LL | / match Some(0) {
|
||||
LL | | Some(_) => Some(2),
|
||||
@ -10,7 +10,7 @@ LL | | };
|
||||
= note: `-D clippy::manual-map` implied by `-D warnings`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:12:5
|
||||
--> $DIR/manual_map_option.rs:19:5
|
||||
|
|
||||
LL | / match Some(0) {
|
||||
LL | | Some(x) => Some(x + 1),
|
||||
@ -19,7 +19,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some(0).map(|x| x + 1)`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:17:5
|
||||
--> $DIR/manual_map_option.rs:24:5
|
||||
|
|
||||
LL | / match Some("") {
|
||||
LL | | Some(x) => Some(x.is_empty()),
|
||||
@ -28,7 +28,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some("").map(|x| x.is_empty())`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:22:5
|
||||
--> $DIR/manual_map_option.rs:29:5
|
||||
|
|
||||
LL | / if let Some(x) = Some(0) {
|
||||
LL | | Some(!x)
|
||||
@ -38,7 +38,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some(0).map(|x| !x)`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:29:5
|
||||
--> $DIR/manual_map_option.rs:36:5
|
||||
|
|
||||
LL | / match Some(0) {
|
||||
LL | | Some(x) => { Some(std::convert::identity(x)) }
|
||||
@ -47,7 +47,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some(0).map(std::convert::identity)`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:34:5
|
||||
--> $DIR/manual_map_option.rs:41:5
|
||||
|
|
||||
LL | / match Some(&String::new()) {
|
||||
LL | | Some(x) => Some(str::len(x)),
|
||||
@ -56,7 +56,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some(&String::new()).map(|x| str::len(x))`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:44:5
|
||||
--> $DIR/manual_map_option.rs:51:5
|
||||
|
|
||||
LL | / match &Some([0, 1]) {
|
||||
LL | | Some(x) => Some(x[0]),
|
||||
@ -65,7 +65,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some([0, 1]).as_ref().map(|x| x[0])`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:49:5
|
||||
--> $DIR/manual_map_option.rs:56:5
|
||||
|
|
||||
LL | / match &Some(0) {
|
||||
LL | | &Some(x) => Some(x * 2),
|
||||
@ -74,7 +74,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some(0).map(|x| x * 2)`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:54:5
|
||||
--> $DIR/manual_map_option.rs:61:5
|
||||
|
|
||||
LL | / match Some(String::new()) {
|
||||
LL | | Some(ref x) => Some(x.is_empty()),
|
||||
@ -83,7 +83,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.is_empty())`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:59:5
|
||||
--> $DIR/manual_map_option.rs:66:5
|
||||
|
|
||||
LL | / match &&Some(String::new()) {
|
||||
LL | | Some(x) => Some(x.len()),
|
||||
@ -92,7 +92,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.len())`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:64:5
|
||||
--> $DIR/manual_map_option.rs:71:5
|
||||
|
|
||||
LL | / match &&Some(0) {
|
||||
LL | | &&Some(x) => Some(x + x),
|
||||
@ -101,7 +101,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some(0).map(|x| x + x)`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:77:9
|
||||
--> $DIR/manual_map_option.rs:84:9
|
||||
|
|
||||
LL | / match &mut Some(String::new()) {
|
||||
LL | | Some(x) => Some(x.push_str("")),
|
||||
@ -110,7 +110,7 @@ LL | | };
|
||||
| |_________^ help: try this: `Some(String::new()).as_mut().map(|x| x.push_str(""))`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:83:5
|
||||
--> $DIR/manual_map_option.rs:90:5
|
||||
|
|
||||
LL | / match &mut Some(String::new()) {
|
||||
LL | | Some(ref x) => Some(x.len()),
|
||||
@ -119,7 +119,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.len())`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:88:5
|
||||
--> $DIR/manual_map_option.rs:95:5
|
||||
|
|
||||
LL | / match &mut &Some(String::new()) {
|
||||
LL | | Some(x) => Some(x.is_empty()),
|
||||
@ -128,7 +128,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.is_empty())`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:93:5
|
||||
--> $DIR/manual_map_option.rs:100:5
|
||||
|
|
||||
LL | / match Some((0, 1, 2)) {
|
||||
LL | | Some((x, y, z)) => Some(x + y + z),
|
||||
@ -137,7 +137,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some((0, 1, 2)).map(|(x, y, z)| x + y + z)`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:98:5
|
||||
--> $DIR/manual_map_option.rs:105:5
|
||||
|
|
||||
LL | / match Some([1, 2, 3]) {
|
||||
LL | | Some([first, ..]) => Some(first),
|
||||
@ -146,7 +146,7 @@ LL | | };
|
||||
| |_____^ help: try this: `Some([1, 2, 3]).map(|[first, ..]| first)`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:103:5
|
||||
--> $DIR/manual_map_option.rs:110:5
|
||||
|
|
||||
LL | / match &Some((String::new(), "test")) {
|
||||
LL | | Some((x, y)) => Some((y, x)),
|
||||
@ -154,5 +154,23 @@ LL | | None => None,
|
||||
LL | | };
|
||||
| |_____^ help: try this: `Some((String::new(), "test")).as_ref().map(|(x, y)| (y, x))`
|
||||
|
||||
error: aborting due to 17 previous errors
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:168:5
|
||||
|
|
||||
LL | / match Some(0) {
|
||||
LL | | Some(x) => Some(vec![x]),
|
||||
LL | | None => None,
|
||||
LL | | };
|
||||
| |_____^ help: try this: `Some(0).map(|x| vec![x])`
|
||||
|
||||
error: manual implementation of `Option::map`
|
||||
--> $DIR/manual_map_option.rs:173:5
|
||||
|
|
||||
LL | / match option_env!("") {
|
||||
LL | | Some(x) => Some(String::from(x)),
|
||||
LL | | None => None,
|
||||
LL | | };
|
||||
| |_____^ help: try this: `option_env!("").map(String::from)`
|
||||
|
||||
error: aborting due to 19 previous errors
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user