Skip to content

Commit

Permalink
Fix dogfood errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Feb 19, 2021
1 parent 1c0ba16 commit 3f997c2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
30 changes: 22 additions & 8 deletions clippy_lints/src/manual_map.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use crate::utils::{
is_type_diagnostic_item, match_def_path, paths, peel_hir_expr_refs, peel_mid_ty_refs_is_mutable,
snippet_with_applicability, span_lint_and_sugg,
use crate::{
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,
},
};
use rustc_ast::util::parser::PREC_POSTFIX;
use rustc_errors::Applicability;
use rustc_hir::{Arm, BindingAnnotation, Block, Expr, ExprKind, Mutability, Pat, PatKind, Path, QPath};
use rustc_hir::{Arm, BindingAnnotation, Block, Expr, ExprKind, Mutability, Pat, PatKind, QPath};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -88,6 +92,12 @@ impl LateLintPass<'_> for ManualMap {
None => return,
};

if cx.typeck_results().expr_ty(some_expr) == cx.tcx.types.unit
&& !is_allowed(cx, OPTION_MAP_UNIT_FN, expr.hir_id)
{
return;
}

// Determine which binding mode to use.
let explicit_ref = some_pat.contains_explicit_ref_binding();
let binding_mutability = explicit_ref.or(if ty_ref_count != pat_ref_count {
Expand Down Expand Up @@ -118,6 +128,13 @@ impl LateLintPass<'_> for ManualMap {
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_mutability.is_some()
{
return;
}

// `ref` and `ref mut` annotations were handled earlier.
let annotation = if matches!(annotation, BindingAnnotation::Mutable) {
"mut "
Expand Down Expand Up @@ -161,10 +178,7 @@ impl LateLintPass<'_> for ManualMap {
fn can_pass_as_func(cx: &LateContext<'tcx>, binding: Ident, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
match expr.kind {
ExprKind::Call(func, [arg])
if matches!(arg.kind,
ExprKind::Path(QPath::Resolved(None, Path { segments: [path], ..}))
if path.ident == binding
) && cx.typeck_results().expr_adjustments(arg).is_empty() =>
if match_var(arg, binding.name) && cx.typeck_results().expr_adjustments(arg).is_empty() =>
{
Some(func)
},
Expand Down
9 changes: 8 additions & 1 deletion tests/ui/manual_map_option.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ fn main() {

Some(0).map(|x| x + x);

#[warn(clippy::option_map_unit_fn)]
match &mut Some(String::new()) {
Some(x) => Some(x.push_str("")),
None => None,
};

#[allow(clippy::option_map_unit_fn)]
Some(String::new()).as_mut().map(|x| x.push_str(""));

Some(String::new()).as_ref().map(|x| &**x);
Some(String::new()).as_ref().map(|x| x.len());

Some(String::new()).as_ref().map(|x| x.is_empty());

Expand Down
9 changes: 8 additions & 1 deletion tests/ui/manual_map_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,20 @@ fn main() {
&&_ => None,
};

#[warn(clippy::option_map_unit_fn)]
match &mut Some(String::new()) {
Some(x) => Some(x.push_str("")),
None => None,
};

#[allow(clippy::option_map_unit_fn)]
match &mut Some(String::new()) {
Some(ref x) => Some(&**x),
Some(x) => Some(x.push_str("")),
None => None,
};

match &mut Some(String::new()) {
Some(ref x) => Some(x.len()),
None => None,
};

Expand Down
16 changes: 8 additions & 8 deletions tests/ui/manual_map_option.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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:69:5
--> $DIR/manual_map_option.rs:76:5
|
LL | / match &mut Some(String::new()) {
LL | | Some(x) => Some(x.push_str("")),
Expand All @@ -110,16 +110,16 @@ 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:74:5
--> $DIR/manual_map_option.rs:81:5
|
LL | / match &mut Some(String::new()) {
LL | | Some(ref x) => Some(&**x),
LL | | Some(ref x) => Some(x.len()),
LL | | None => None,
LL | | };
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| &**x)`
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.len())`

error: manual implementation of `Option::map`
--> $DIR/manual_map_option.rs:79:5
--> $DIR/manual_map_option.rs:86:5
|
LL | / match &mut &Some(String::new()) {
LL | | Some(x) => Some(x.is_empty()),
Expand All @@ -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:84:5
--> $DIR/manual_map_option.rs:91:5
|
LL | / match Some((0, 1, 2)) {
LL | | Some((x, y, z)) => Some(x + y + z),
Expand All @@ -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:89:5
--> $DIR/manual_map_option.rs:96:5
|
LL | / match Some([1, 2, 3]) {
LL | | Some([first, ..]) => Some(first),
Expand All @@ -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:94:5
--> $DIR/manual_map_option.rs:101:5
|
LL | / match &Some((String::new(), "test")) {
LL | | Some((x, y)) => Some((y, x)),
Expand Down

0 comments on commit 3f997c2

Please sign in to comment.