Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions clippy_lints/src/map_unit_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ fn lint_map_unit_fn(
};
let fn_arg = &map_args.1[0];

#[expect(clippy::items_after_statements, reason = "the const is only used below")]
const SUGG_MSG: &str = "use `if let` instead";

if is_unit_function(cx, fn_arg) {
let mut applicability = Applicability::MachineApplicable;
let msg = suggestion_msg("function", map_type);
Expand All @@ -226,7 +229,7 @@ fn lint_map_unit_fn(
);

span_lint_and_then(cx, lint, expr.span, msg, |diag| {
diag.span_suggestion(stmt.span, "try", suggestion, applicability);
diag.span_suggestion_verbose(stmt.span, SUGG_MSG, suggestion, applicability);
});
} else if let Some((binding, closure_expr)) = unit_closure(cx, fn_arg) {
let msg = suggestion_msg("closure", map_type);
Expand All @@ -242,15 +245,15 @@ fn lint_map_unit_fn(
snippet_with_applicability(cx, var_arg.span, "_", &mut applicability),
snippet_with_context(cx, reduced_expr_span, var_arg.span.ctxt(), "_", &mut applicability).0,
);
diag.span_suggestion(stmt.span, "try", suggestion, applicability);
diag.span_suggestion_verbose(stmt.span, SUGG_MSG, suggestion, applicability);
} else {
let suggestion = format!(
"if let {0}({1}) = {2} {{ ... }}",
variant,
snippet(cx, binding.pat.span, "_"),
snippet(cx, var_arg.span, "_"),
);
diag.span_suggestion(stmt.span, "try", suggestion, Applicability::HasPlaceholders);
diag.span_suggestion_verbose(stmt.span, SUGG_MSG, suggestion, Applicability::HasPlaceholders);
}
});
}
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/option_map_unit_fn_fixable.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![warn(clippy::option_map_unit_fn)]
#![allow(unused)]
#![allow(clippy::uninlined_format_args, clippy::unnecessary_wraps)]
#![expect(clippy::unnecessary_wraps)]

fn do_nothing<T>(_: T) {}

Expand Down Expand Up @@ -98,7 +97,7 @@ fn option_map_unit_fn() {
if let Some(a) = option() { do_nothing(a) }
//~^ option_map_unit_fn

if let Some(value) = option() { println!("{:?}", value) }
if let Some(value) = option() { println!("{value:?}") }
//~^ option_map_unit_fn
}

Expand Down
5 changes: 2 additions & 3 deletions tests/ui/option_map_unit_fn_fixable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![warn(clippy::option_map_unit_fn)]
#![allow(unused)]
#![allow(clippy::uninlined_format_args, clippy::unnecessary_wraps)]
#![expect(clippy::unnecessary_wraps)]

fn do_nothing<T>(_: T) {}

Expand Down Expand Up @@ -98,7 +97,7 @@ fn option_map_unit_fn() {
option().map(do_nothing);
//~^ option_map_unit_fn

option().map(|value| println!("{:?}", value));
option().map(|value| println!("{value:?}"));
//~^ option_map_unit_fn
}

Expand Down
Loading