Skip to content

Commit a2283e0

Browse files
author
Yusuf Raji
committed
Fix test case for scenario that shouldn't be linted
1 parent b0de53c commit a2283e0

4 files changed

Lines changed: 30 additions & 3 deletions

File tree

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,5 +977,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
977977
store.register_late_pass(|_| Box::new(unneeded_struct_pattern::UnneededStructPattern));
978978
store.register_late_pass(|_| Box::<unnecessary_semicolon::UnnecessarySemicolon>::default());
979979
store.register_late_pass(move |_| Box::new(non_std_lazy_statics::NonStdLazyStatic::new(conf)));
980+
store.register_late_pass(|_| Box::new(single_option_map::SingleOptionMap));
980981
// add lints here, do not remove this comment, it's used in `new_lint`
981982
}

clippy_lints/src/single_option_map.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ impl<'tcx> LateLintPass<'tcx> for SingleOptionMap {
5252
&& matches!(kind, FnKind::ItemFn(_, _, _) | FnKind::Method(_, _))
5353
{
5454
let func_body = peel_blocks(body.value);
55-
if let ExprKind::MethodCall(method_name, callee, _args, _span) = func_body.kind
55+
if let ExprKind::MethodCall(method_name, callee, args, _span) = func_body.kind
5656
&& method_name.ident.name == sym::map
5757
&& let callee_type = cx.typeck_results().expr_ty(callee)
5858
&& is_type_diagnostic_item(cx, callee_type, sym::Option)
5959
&& let ExprKind::Path(_path) = callee.kind
6060
&& let Res::Local(_id) = path_res(cx, callee)
61+
&& !matches!(args[0].kind, ExprKind::Path(_))
6162
{
6263
span_lint_and_help(
6364
cx,

tests/ui/single_option_map.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
use std::sync::atomic::{AtomicUsize, Ordering};
44

5-
static MAYBE_ATOMIC: Option<AtomicUsize> = Some(AtomicUsize::new(42));
5+
static ATOM: AtomicUsize = AtomicUsize::new(42);
6+
static MAYBE_ATOMIC: Option<&AtomicUsize> = Some(&ATOM);
67

78
fn h(arg: Option<u32>) -> Option<u32> {
89
arg.map(|x| x * 2)
@@ -13,7 +14,7 @@ fn j(arg: Option<u64>) -> Option<u64> {
1314
}
1415

1516
fn maps_static_option() -> Option<usize> {
16-
MAYBE_ATOMIC.as_ref().map(|a| a.load(Ordering::Relaxed))
17+
MAYBE_ATOMIC.map(|a| a.load(Ordering::Relaxed))
1718
}
1819

1920
fn manipulate(i: i32) -> i32 {

tests/ui/single_option_map.stderr

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: `fn` that only maps over argument
2+
--> tests/ui/single_option_map.rs:8:1
3+
|
4+
LL | / fn h(arg: Option<u32>) -> Option<u32> {
5+
LL | | arg.map(|x| x * 2)
6+
LL | | }
7+
| |_^
8+
|
9+
= help: move the `.map` to the caller or to an `_opt` function
10+
= note: `-D clippy::single-option-map` implied by `-D warnings`
11+
= help: to override `-D warnings` add `#[allow(clippy::single_option_map)]`
12+
13+
error: `fn` that only maps over argument
14+
--> tests/ui/single_option_map.rs:12:1
15+
|
16+
LL | / fn j(arg: Option<u64>) -> Option<u64> {
17+
LL | | arg.map(|x| x * 2)
18+
LL | | }
19+
| |_^
20+
|
21+
= help: move the `.map` to the caller or to an `_opt` function
22+
23+
error: aborting due to 2 previous errors
24+

0 commit comments

Comments
 (0)