Skip to content

Commit 188df9f

Browse files
committed
Fix dogfood errors
1 parent 1c0ba16 commit 188df9f

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

clippy_lints/src/manual_map.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::utils::{
22
is_type_diagnostic_item, match_def_path, paths, peel_hir_expr_refs, peel_mid_ty_refs_is_mutable,
3-
snippet_with_applicability, span_lint_and_sugg,
3+
snippet_with_applicability, span_lint_and_sugg, match_var, is_allowed,
44
};
5+
use crate::{matches::MATCH_AS_REF, map_unit_fn::OPTION_MAP_UNIT_FN};
56
use rustc_ast::util::parser::PREC_POSTFIX;
67
use rustc_errors::Applicability;
7-
use rustc_hir::{Arm, BindingAnnotation, Block, Expr, ExprKind, Mutability, Pat, PatKind, Path, QPath};
8+
use rustc_hir::{Arm, BindingAnnotation, Block, Expr, ExprKind, Mutability, Pat, PatKind, QPath};
89
use rustc_lint::{LateContext, LateLintPass, LintContext};
910
use rustc_middle::lint::in_external_macro;
1011
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -88,6 +89,11 @@ impl LateLintPass<'_> for ManualMap {
8889
None => return,
8990
};
9091

92+
if cx.typeck_results().expr_ty(some_expr) == cx.tcx.types.unit
93+
&& !is_allowed(cx, OPTION_MAP_UNIT_FN, expr.hir_id) {
94+
return;
95+
}
96+
9197
// Determine which binding mode to use.
9298
let explicit_ref = some_pat.contains_explicit_ref_binding();
9399
let binding_mutability = explicit_ref.or(if ty_ref_count != pat_ref_count {
@@ -118,6 +124,10 @@ impl LateLintPass<'_> for ManualMap {
118124
if let Some(func) = can_pass_as_func(cx, some_binding, some_expr) {
119125
snippet_with_applicability(cx, func.span, "..", &mut app).into_owned()
120126
} else {
127+
if match_var(some_expr, some_binding.name) && !is_allowed(cx, MATCH_AS_REF, expr.hir_id) && binding_mutability.is_some() {
128+
return;
129+
}
130+
121131
// `ref` and `ref mut` annotations were handled earlier.
122132
let annotation = if matches!(annotation, BindingAnnotation::Mutable) {
123133
"mut "
@@ -161,10 +171,7 @@ impl LateLintPass<'_> for ManualMap {
161171
fn can_pass_as_func(cx: &LateContext<'tcx>, binding: Ident, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
162172
match expr.kind {
163173
ExprKind::Call(func, [arg])
164-
if matches!(arg.kind,
165-
ExprKind::Path(QPath::Resolved(None, Path { segments: [path], ..}))
166-
if path.ident == binding
167-
) && cx.typeck_results().expr_adjustments(arg).is_empty() =>
174+
if match_var(arg, binding.name) && cx.typeck_results().expr_adjustments(arg).is_empty() =>
168175
{
169176
Some(func)
170177
},

tests/ui/manual_map_option.fixed

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ fn main() {
3232

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

35+
#[warn(clippy::option_map_unit_fn)]
36+
match &mut Some(String::new()) {
37+
Some(x) => Some(x.push_str("")),
38+
None => None,
39+
};
40+
41+
#[allow(clippy::option_map_unit_fn)]
3542
Some(String::new()).as_mut().map(|x| x.push_str(""));
3643

37-
Some(String::new()).as_ref().map(|x| &**x);
44+
Some(String::new()).as_ref().map(|x| x.len());
3845

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

tests/ui/manual_map_option.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,20 @@ fn main() {
6666
&&_ => None,
6767
};
6868

69+
#[warn(clippy::option_map_unit_fn)]
6970
match &mut Some(String::new()) {
7071
Some(x) => Some(x.push_str("")),
7172
None => None,
7273
};
7374

75+
#[allow(clippy::option_map_unit_fn)]
7476
match &mut Some(String::new()) {
75-
Some(ref x) => Some(&**x),
77+
Some(x) => Some(x.push_str("")),
78+
None => None,
79+
};
80+
81+
match &mut Some(String::new()) {
82+
Some(ref x) => Some(x.len()),
7683
None => None,
7784
};
7885

tests/ui/manual_map_option.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ LL | | };
101101
| |_____^ help: try this: `Some(0).map(|x| x + x)`
102102

103103
error: manual implementation of `Option::map`
104-
--> $DIR/manual_map_option.rs:69:5
104+
--> $DIR/manual_map_option.rs:76:5
105105
|
106106
LL | / match &mut Some(String::new()) {
107107
LL | | Some(x) => Some(x.push_str("")),
@@ -110,16 +110,16 @@ LL | | };
110110
| |_____^ help: try this: `Some(String::new()).as_mut().map(|x| x.push_str(""))`
111111

112112
error: manual implementation of `Option::map`
113-
--> $DIR/manual_map_option.rs:74:5
113+
--> $DIR/manual_map_option.rs:81:5
114114
|
115115
LL | / match &mut Some(String::new()) {
116-
LL | | Some(ref x) => Some(&**x),
116+
LL | | Some(ref x) => Some(x.len()),
117117
LL | | None => None,
118118
LL | | };
119-
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| &**x)`
119+
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.len())`
120120

121121
error: manual implementation of `Option::map`
122-
--> $DIR/manual_map_option.rs:79:5
122+
--> $DIR/manual_map_option.rs:86:5
123123
|
124124
LL | / match &mut &Some(String::new()) {
125125
LL | | Some(x) => Some(x.is_empty()),
@@ -128,7 +128,7 @@ LL | | };
128128
| |_____^ help: try this: `Some(String::new()).as_ref().map(|x| x.is_empty())`
129129

130130
error: manual implementation of `Option::map`
131-
--> $DIR/manual_map_option.rs:84:5
131+
--> $DIR/manual_map_option.rs:91:5
132132
|
133133
LL | / match Some((0, 1, 2)) {
134134
LL | | Some((x, y, z)) => Some(x + y + z),
@@ -137,7 +137,7 @@ LL | | };
137137
| |_____^ help: try this: `Some((0, 1, 2)).map(|(x, y, z)| x + y + z)`
138138

139139
error: manual implementation of `Option::map`
140-
--> $DIR/manual_map_option.rs:89:5
140+
--> $DIR/manual_map_option.rs:96:5
141141
|
142142
LL | / match Some([1, 2, 3]) {
143143
LL | | Some([first, ..]) => Some(first),
@@ -146,7 +146,7 @@ LL | | };
146146
| |_____^ help: try this: `Some([1, 2, 3]).map(|[first, ..]| first)`
147147

148148
error: manual implementation of `Option::map`
149-
--> $DIR/manual_map_option.rs:94:5
149+
--> $DIR/manual_map_option.rs:101:5
150150
|
151151
LL | / match &Some((String::new(), "test")) {
152152
LL | | Some((x, y)) => Some((y, x)),

0 commit comments

Comments
 (0)