Skip to content

Commit b0f0e62

Browse files
committed
Suggest Option<&T> instead of &Option<T>
1 parent b367d34 commit b0f0e62

16 files changed

+679
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5871,6 +5871,7 @@ Released 2018-09-13
58715871
[`ref_as_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_as_ptr
58725872
[`ref_binding_to_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_binding_to_reference
58735873
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
5874+
[`ref_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_option
58745875
[`ref_option_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_option_ref
58755876
[`ref_patterns`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_patterns
58765877
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro

book/src/lint_configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ Suppress lints whenever the suggested change would cause breakage for other crat
353353
* [`rc_buffer`](https://rust-lang.github.io/rust-clippy/master/index.html#rc_buffer)
354354
* [`rc_mutex`](https://rust-lang.github.io/rust-clippy/master/index.html#rc_mutex)
355355
* [`redundant_allocation`](https://rust-lang.github.io/rust-clippy/master/index.html#redundant_allocation)
356+
* [`ref_option`](https://rust-lang.github.io/rust-clippy/master/index.html#ref_option)
356357
* [`single_call_fn`](https://rust-lang.github.io/rust-clippy/master/index.html#single_call_fn)
357358
* [`trivially_copy_pass_by_ref`](https://rust-lang.github.io/rust-clippy/master/index.html#trivially_copy_pass_by_ref)
358359
* [`unnecessary_box_returns`](https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_box_returns)

clippy_config/src/conf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ define_Conf! {
378378
rc_buffer,
379379
rc_mutex,
380380
redundant_allocation,
381+
ref_option,
381382
single_call_fn,
382383
trivially_copy_pass_by_ref,
383384
unnecessary_box_returns,

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
202202
crate::functions::MUST_USE_CANDIDATE_INFO,
203203
crate::functions::MUST_USE_UNIT_INFO,
204204
crate::functions::NOT_UNSAFE_PTR_ARG_DEREF_INFO,
205+
crate::functions::REF_OPTION_INFO,
205206
crate::functions::RENAMED_FUNCTION_PARAMS_INFO,
206207
crate::functions::RESULT_LARGE_ERR_INFO,
207208
crate::functions::RESULT_UNIT_ERR_INFO,

clippy_lints/src/functions/mod.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod impl_trait_in_params;
22
mod misnamed_getters;
33
mod must_use;
44
mod not_unsafe_ptr_arg_deref;
5+
mod ref_option;
56
mod renamed_function_params;
67
mod result;
78
mod too_many_arguments;
@@ -399,6 +400,34 @@ declare_clippy_lint! {
399400
"renamed function parameters in trait implementation"
400401
}
401402

403+
declare_clippy_lint! {
404+
/// ### What it does
405+
/// Warns when a function signature uses `&Option<T>` instead of `Option<&T>`.
406+
/// ### Why is this bad?
407+
/// More flexibility, better memory optimization, and more idiomatic Rust code.
408+
/// See this [YouTube video](https://www.youtube.com/watch?v=6c7pZYP_iIE)
409+
/// ### Example
410+
/// ```no_run
411+
/// fn foo(a: &Option<String>) {}
412+
/// # struct Unit {}
413+
/// # impl Unit {
414+
/// fn bar(&self) -> &Option<String> { &None }
415+
/// # }
416+
/// ```
417+
/// Use instead:
418+
/// ```no_run
419+
/// fn foo(a: Option<&String>) {}
420+
/// # struct Unit {}
421+
/// # impl Unit {
422+
/// fn bar(&self) -> Option<&String> { None }
423+
/// # }
424+
/// ```
425+
#[clippy::version = "1.82.0"]
426+
pub REF_OPTION,
427+
nursery,
428+
"function signature uses `&Option<T>` instead of `Option<&T>`"
429+
}
430+
402431
pub struct Functions {
403432
too_many_arguments_threshold: u64,
404433
too_many_lines_threshold: u64,
@@ -437,6 +466,7 @@ impl_lint_pass!(Functions => [
437466
MISNAMED_GETTERS,
438467
IMPL_TRAIT_IN_PARAMS,
439468
RENAMED_FUNCTION_PARAMS,
469+
REF_OPTION,
440470
]);
441471

442472
impl<'tcx> LateLintPass<'tcx> for Functions {
@@ -455,6 +485,16 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
455485
not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, def_id);
456486
misnamed_getters::check_fn(cx, kind, decl, body, span);
457487
impl_trait_in_params::check_fn(cx, &kind, body, hir_id);
488+
ref_option::check_fn(
489+
cx,
490+
kind,
491+
decl,
492+
span,
493+
hir_id,
494+
def_id,
495+
body,
496+
self.avoid_breaking_exported_api,
497+
);
458498
}
459499

460500
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
@@ -475,5 +515,6 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
475515
must_use::check_trait_item(cx, item);
476516
result::check_trait_item(cx, item, self.large_error_threshold);
477517
impl_trait_in_params::check_trait_item(cx, item, self.avoid_breaking_exported_api);
518+
ref_option::check_trait_item(cx, item, self.avoid_breaking_exported_api);
478519
}
479520
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
use crate::functions::REF_OPTION;
2+
use clippy_utils::diagnostics::span_lint_and_then;
3+
use clippy_utils::is_trait_impl_item;
4+
use clippy_utils::source::snippet;
5+
use clippy_utils::ty::is_type_diagnostic_item;
6+
use rustc_errors::Applicability;
7+
use rustc_hir as hir;
8+
use rustc_hir::intravisit::FnKind;
9+
use rustc_hir::{FnDecl, HirId};
10+
use rustc_lint::LateContext;
11+
use rustc_middle::ty::{self, GenericArgKind, Mutability, Ty};
12+
use rustc_span::def_id::LocalDefId;
13+
use rustc_span::{Span, sym};
14+
15+
fn check_ty<'a>(cx: &LateContext<'a>, param: &rustc_hir::Ty<'a>, param_ty: Ty<'a>, fixes: &mut Vec<(Span, String)>) {
16+
if let ty::Ref(_, opt_ty, Mutability::Not) = param_ty.kind()
17+
&& is_type_diagnostic_item(cx, *opt_ty, sym::Option)
18+
&& let ty::Adt(_, opt_gen) = opt_ty.kind()
19+
&& let [gen] = opt_gen.as_slice()
20+
&& let GenericArgKind::Type(gen_ty) = gen.unpack()
21+
&& !gen_ty.is_ref()
22+
// Need to gen the original spans, so first parsing mid, and hir parsing afterward
23+
&& let hir::TyKind::Ref(lifetime, hir::MutTy { ty, .. }) = param.kind
24+
&& let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = ty.kind
25+
&& let (Some(first), Some(last)) = (path.segments.first(), path.segments.last())
26+
&& let Some(hir::GenericArgs {
27+
args: [hir::GenericArg::Type(opt_ty)],
28+
..
29+
}) = last.args
30+
{
31+
let lifetime = snippet(cx, lifetime.ident.span, "..");
32+
fixes.push((
33+
param.span,
34+
format!(
35+
"{}<&{lifetime}{}{}>",
36+
snippet(cx, first.ident.span.to(last.ident.span), ".."),
37+
if lifetime.is_empty() { "" } else { " " },
38+
snippet(cx, opt_ty.span, "..")
39+
),
40+
));
41+
}
42+
}
43+
44+
fn check_fn_sig<'a>(cx: &LateContext<'a>, decl: &FnDecl<'a>, span: Span, sig: ty::FnSig<'a>) {
45+
let mut fixes = Vec::new();
46+
// Check function arguments' types
47+
for (param, param_ty) in decl.inputs.iter().zip(sig.inputs()) {
48+
check_ty(cx, param, *param_ty, &mut fixes);
49+
}
50+
// Check return type
51+
if let hir::FnRetTy::Return(ty) = &decl.output {
52+
check_ty(cx, ty, sig.output(), &mut fixes);
53+
}
54+
if !fixes.is_empty() {
55+
span_lint_and_then(
56+
cx,
57+
REF_OPTION,
58+
span,
59+
"it is more idiomatic to use `Option<&T>` instead of `&Option<T>`",
60+
|diag| {
61+
diag.multipart_suggestion("change this to", fixes, Applicability::Unspecified);
62+
},
63+
);
64+
}
65+
}
66+
67+
pub(crate) fn check_fn<'a>(
68+
cx: &LateContext<'a>,
69+
kind: FnKind<'_>,
70+
decl: &FnDecl<'a>,
71+
span: Span,
72+
hir_id: HirId,
73+
def_id: LocalDefId,
74+
body: &hir::Body<'_>,
75+
avoid_breaking_exported_api: bool,
76+
) {
77+
if avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id) {
78+
return;
79+
}
80+
81+
if let FnKind::Closure = kind {
82+
// Compute the span of the closure parameters + return type if set
83+
let span = if let hir::FnRetTy::Return(out_ty) = &decl.output {
84+
if decl.inputs.is_empty() {
85+
out_ty.span
86+
} else {
87+
span.with_hi(out_ty.span.hi())
88+
}
89+
} else if let (Some(first), Some(last)) = (decl.inputs.first(), decl.inputs.last()) {
90+
first.span.to(last.span)
91+
} else {
92+
// No parameters - no point in checking
93+
return;
94+
};
95+
96+
// Figure out the signature of the closure
97+
let ty::Closure(_, args) = cx.typeck_results().expr_ty(body.value).kind() else {
98+
return;
99+
};
100+
let sig = args.as_closure().sig().skip_binder();
101+
102+
check_fn_sig(cx, decl, span, sig);
103+
} else if !is_trait_impl_item(cx, hir_id) {
104+
let sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder();
105+
check_fn_sig(cx, decl, span, sig);
106+
}
107+
}
108+
109+
pub(super) fn check_trait_item<'a>(
110+
cx: &LateContext<'a>,
111+
trait_item: &hir::TraitItem<'a>,
112+
avoid_breaking_exported_api: bool,
113+
) {
114+
if let hir::TraitItemKind::Fn(ref sig, _) = trait_item.kind
115+
&& !(avoid_breaking_exported_api && cx.effective_visibilities.is_exported(trait_item.owner_id.def_id))
116+
{
117+
let def_id = trait_item.owner_id.def_id;
118+
let ty_sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder();
119+
check_fn_sig(cx, sig.decl, sig.span, ty_sig);
120+
}
121+
}

tests/ui/ref_option/all/clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
avoid-breaking-exported-api = false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
avoid-breaking-exported-api = true
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//@revisions: private all
2+
//@[private] rustc-env:CLIPPY_CONF_DIR=tests/ui/ref_option/private
3+
//@[all] rustc-env:CLIPPY_CONF_DIR=tests/ui/ref_option/all
4+
5+
#![allow(unused, clippy::all)]
6+
#![warn(clippy::ref_option)]
7+
8+
fn opt_u8(a: Option<&u8>) {}
9+
fn opt_gen<T>(a: Option<&T>) {}
10+
fn opt_string(a: std::option::Option<&String>) {}
11+
fn ret_string<'a>(p: &'a String) -> Option<&'a u8> {
12+
panic!()
13+
}
14+
fn ret_string_static() -> Option<&'static u8> {
15+
panic!()
16+
}
17+
fn mult_string(a: Option<&String>, b: Option<&Vec<u8>>) {}
18+
fn mut_u8(a: &mut Option<u8>) {} // valid, don't change
19+
fn mut_u8_ref(a: &mut &Option<u8>) {} // probably valid, don't change
20+
21+
pub fn pub_opt_string(a: Option<&String>) {}
22+
pub fn pub_mult_string(a: Option<&String>, b: Option<&Vec<u8>>) {}
23+
pub fn pub_mut_u8(a: &mut Option<String>) {} // valid, don't change
24+
pub fn pub_mut_u8_ref(a: &mut &Option<String>) {} // probably valid, don't change
25+
26+
pub trait PubTrait {
27+
fn pub_trait_opt(&self, a: Option<&Vec<u8>>);
28+
fn pub_trait_ret(&self) -> Option<&Vec<u8>>;
29+
}
30+
31+
trait PrivateTrait {
32+
fn trait_opt(&self, a: Option<&String>);
33+
fn trait_ret(&self) -> Option<&String>;
34+
}
35+
36+
pub struct PubStruct;
37+
38+
impl PubStruct {
39+
pub fn pub_opt_params(&self, a: Option<&()>) {}
40+
pub fn pub_opt_ret(&self) -> Option<&String> {
41+
panic!()
42+
}
43+
44+
fn private_opt_params(&self, a: Option<&()>) {}
45+
fn private_opt_ret(&self) -> Option<&String> {
46+
panic!()
47+
}
48+
}
49+
50+
fn lambdas() {
51+
// Not handled for now, not sure if we should
52+
let x = |a: &Option<String>| {};
53+
let x = |a: &Option<String>| -> &Option<String> { panic!() };
54+
}
55+
56+
fn main() {}

0 commit comments

Comments
 (0)