Skip to content

Commit 7e2043d

Browse files
committed
Ignore all enum and struct constructors in lints about *or(call())
1 parent d420589 commit 7e2043d

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 10 additions & 5 deletions
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

Lines changed: 13 additions & 0 deletions
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 {

0 commit comments

Comments
 (0)