Skip to content

Commit 9897442

Browse files
committed
Auto merge of #4018 - rust-lang:or_fn_call_variants, r=oli-obk
Ignore non-const ctor expressions in or_fn_call Fixes rust-lang/rust-clippy#1338 Should have been fixed by #919, however that focuses on const ctor expressions only, and `.or(Some(local))` isn't const. This also automatically ignores things like `.or(Some(local.clone())` which we don't actually want to do; I need to figure out what to do here. changelog: Fixed false positive in [`or_fn_call`] pertaining to enum variant constructors r? @oli-obk @phansch
2 parents 65d60e9 + b03cf3f commit 9897442

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

clippy_lints/src/methods/mod.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ use syntax::symbol::LocalInternedString;
2020
use crate::utils::paths;
2121
use crate::utils::sugg;
2222
use crate::utils::{
23-
get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro, is_copy, is_expn_of,
24-
is_self, is_self_ty, iter_input_pats, last_path_segment, match_path, match_qpath, match_trait_method, match_type,
25-
match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys, single_segment_path, snippet,
26-
snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_sugg, span_lint_and_then,
27-
span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq,
23+
get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro, is_copy,
24+
is_ctor_function, is_expn_of, is_self, is_self_ty, iter_input_pats, last_path_segment, match_path, match_qpath,
25+
match_trait_method, match_type, match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys,
26+
single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
27+
span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq,
2828
};
2929

3030
declare_clippy_lint! {
@@ -1072,6 +1072,11 @@ fn lint_or_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: Spa
10721072
return;
10731073
}
10741074

1075+
// ignore enum and struct constructors
1076+
if is_ctor_function(cx, &arg) {
1077+
return;
1078+
}
1079+
10751080
// don't lint for constant values
10761081
let owner_def = cx.tcx.hir().get_parent_did_by_hir_id(arg.hir_id);
10771082
let promotable = cx.tcx.rvalue_promotable_map(owner_def).contains(&arg.hir_id.local_id);

clippy_lints/src/utils/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,19 @@ pub fn is_copy<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
742742
ty.is_copy_modulo_regions(cx.tcx.global_tcx(), cx.param_env, DUMMY_SP)
743743
}
744744

745+
/// Checks if an expression is constructing a tuple-like enum variant or struct
746+
pub fn is_ctor_function(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
747+
if let ExprKind::Call(ref fun, _) = expr.node {
748+
if let ExprKind::Path(ref qp) = fun.node {
749+
return matches!(
750+
cx.tables.qpath_def(qp, fun.hir_id),
751+
def::Def::Variant(..) | def::Def::Ctor(..)
752+
);
753+
}
754+
}
755+
false
756+
}
757+
745758
/// Returns `true` if a pattern is refutable.
746759
pub fn is_refutable(cx: &LateContext<'_, '_>, pat: &Pat) -> bool {
747760
fn is_enum_variant(cx: &LateContext<'_, '_>, qpath: &QPath, id: HirId) -> bool {

tests/ui/methods.rs

+18
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,21 @@ fn main() {
268268
let opt = Some(0);
269269
let _ = opt.unwrap();
270270
}
271+
272+
struct Foo(u8);
273+
#[rustfmt::skip]
274+
fn test_or_with_ctors() {
275+
let opt = Some(1);
276+
let opt_opt = Some(Some(1));
277+
// we also test for const promotion, this makes sure we don't hit that
278+
let two = 2;
279+
280+
let _ = opt_opt.unwrap_or(Some(2));
281+
let _ = opt_opt.unwrap_or(Some(two));
282+
let _ = opt.ok_or(Some(2));
283+
let _ = opt.ok_or(Some(two));
284+
let _ = opt.ok_or(Foo(2));
285+
let _ = opt.ok_or(Foo(two));
286+
let _ = opt.or(Some(2));
287+
let _ = opt.or(Some(two));
288+
}

0 commit comments

Comments
 (0)