Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed May 9, 2024
1 parent befb659 commit d44c289
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 5 deletions.
45 changes: 41 additions & 4 deletions clippy_lints/src/matches/manual_utils.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use std::ops::ControlFlow;
use crate::map_unit_fn::OPTION_MAP_UNIT_FN;
use crate::matches::MATCH_AS_REF;
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{is_copy, is_type_diagnostic_item, peel_mid_ty_refs_is_mutable, type_is_unsafe_function};
use clippy_utils::visitors::for_each_expr;
use clippy_utils::{
can_move_expr_to_closure, is_else_clause, is_lint_allowed, is_res_lang_ctor, path_res, path_to_local_id,
peel_blocks, peel_hir_expr_refs, peel_hir_expr_while, CaptureKind,
can_move_expr_to_closure, is_else_clause, is_lint_allowed, is_res_lang_ctor, path_res, path_to_local, path_to_local_id, peel_blocks, peel_hir_expr_refs, peel_hir_expr_while, CaptureKind
};
use rustc_ast::util::parser::PREC_POSTFIX;
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::LangItem::{OptionNone, OptionSome};
use rustc_hir::{BindingMode, Expr, ExprKind, HirId, Mutability, Pat, PatKind, Path, QPath};
use rustc_hir::{BindingMode, Expr, ExprKind, HirId, LetStmt, Mutability, Node, Pat, PatKind, Path, QPath, TyKind};
use rustc_lint::LateContext;
use rustc_span::{sym, SyntaxContext};

Expand Down Expand Up @@ -73,7 +74,7 @@ where
}

// `map` won't perform any adjustments.
if !cx.typeck_results().expr_adjustments(some_expr.expr).is_empty() {
if expr_ty_is_explicitly_labeled(cx, expr) && some_expr.expr_contains_ty_adjustments(cx) {
return None;
}

Expand Down Expand Up @@ -238,6 +239,16 @@ impl<'tcx> SomeExpr<'tcx> {
let sugg = Sugg::hir_with_context(cx, self.expr, ctxt, "..", app);
if self.needs_negated { !sugg } else { sugg }
}

fn expr_contains_ty_adjustments(&self, cx: &LateContext<'tcx>) -> bool {
for_each_expr(self.expr, |ex| {
if !cx.typeck_results().expr_adjustments(ex).is_empty() {
ControlFlow::Break(true)
} else {
ControlFlow::Continue(())
}
}).unwrap_or_default()
}
}

// Try to parse into a recognized `Option` pattern.
Expand Down Expand Up @@ -274,3 +285,29 @@ pub(super) fn try_parse_pattern<'tcx>(
fn is_none_expr(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
is_res_lang_ctor(cx, path_res(cx, peel_blocks(expr)), OptionNone)
}

/// Return `true` if a given expr's type is labeled explicitly.
///
/// Such as the init binding of:
///
/// ```rust
/// let x: &[u8;5] = b"hello";
/// // ^^^^^^^^
/// ```
///
/// Or the return of a function:
///
/// ```rust
/// fn foo() -> u8 {
/// 0
/// // ^
/// }
/// ```
fn expr_ty_is_explicitly_labeled(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
let parent = cx.tcx.parent_hir_node(expr.hir_id);
if let Node::LetStmt(LetStmt { ty: None, .. }) = parent {
false
} else {
true
}
}
37 changes: 37 additions & 0 deletions tests/ui/manual_map_option_2.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,40 @@ fn main() {
let _ = Some(0).map(|x| unsafe { f(x) });
let _ = Some(0).map(|x| unsafe { f(x) });
}

mod issue_12659 {
trait DummyTrait {}

fn foo<T: DummyTrait, F: Fn() -> Result<T, ()>>(f: F) {
// Don't lint
let _: Option<Result<Box<dyn DummyTrait>, ()>> = match Some(0) {
Some(_) => Some(match f() {
Ok(res) => Ok(Box::new(res)),
_ => Err(()),
}),
None => None,
};

let _: Option<Box<&[u8]>> = match Some(()) {
Some(_) => Some(Box::new(b"1234")),
None => None,
};

let x = String::new();
let _: Option<Box<&str>> = match Some(()) {
Some(_) => Some(Box::new(&x)),
None => None,
};

let _: Option<&str> = match Some(()) {
Some(_) => Some(&x),
None => None,
};

//~v ERROR: manual implementation of `Option::map`
let _ = Some(0).map(|_| match f() {
Ok(res) => Ok(Box::new(res)),
_ => Err(()),
});
}
}
40 changes: 40 additions & 0 deletions tests/ui/manual_map_option_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,43 @@ fn main() {
None => None,
};
}

mod issue_12659 {
trait DummyTrait {}

fn foo<T: DummyTrait, F: Fn() -> Result<T, ()>>(f: F) {
// Don't lint
let _: Option<Result<Box<dyn DummyTrait>, ()>> = match Some(0) {
Some(_) => Some(match f() {
Ok(res) => Ok(Box::new(res)),
_ => Err(()),
}),
None => None,
};

let _: Option<Box<&[u8]>> = match Some(()) {
Some(_) => Some(Box::new(b"1234")),
None => None,
};

let x = String::new();
let _: Option<Box<&str>> = match Some(()) {
Some(_) => Some(Box::new(&x)),
None => None,
};

let _: Option<&str> = match Some(()) {
Some(_) => Some(&x),
None => None,
};

//~v ERROR: manual implementation of `Option::map`
let _ = match Some(0) {
Some(_) => Some(match f() {
Ok(res) => Ok(Box::new(res)),
_ => Err(()),
}),
None => None,
};
}
}
23 changes: 22 additions & 1 deletion tests/ui/manual_map_option_2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,26 @@ LL | | None => None,
LL | | };
| |_____^ help: try: `Some(0).map(|x| unsafe { f(x) })`

error: aborting due to 5 previous errors
error: manual implementation of `Option::map`
--> tests/ui/manual_map_option_2.rs:103:17
|
LL | let _ = match Some(0) {
| _________________^
LL | | Some(_) => Some(match f() {
LL | | Ok(res) => Ok(Box::new(res)),
LL | | _ => Err(()),
LL | | }),
LL | | None => None,
LL | | };
| |_________^
|
help: try
|
LL ~ let _ = Some(0).map(|_| match f() {
LL + Ok(res) => Ok(Box::new(res)),
LL + _ => Err(()),
LL ~ });
|

error: aborting due to 6 previous errors

0 comments on commit d44c289

Please sign in to comment.