Skip to content

Commit e750c55

Browse files
committed
Update clippy
1 parent d6b54d6 commit e750c55

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
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/misc_early.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl EarlyLintPass for MiscEarlyLints {
223223
}
224224
}
225225

226-
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat, _: &mut bool) {
226+
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat) {
227227
if let PatKind::Struct(ref npat, ref pfields, _) = pat.node {
228228
let mut wilds = 0;
229229
let type_name = npat

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 {

tests/compile-test.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,32 @@ fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
4646
config.run_lib_path = rustc_lib_path();
4747
config.compile_lib_path = rustc_lib_path();
4848
}
49+
50+
// When we'll want to use `extern crate ..` for a dependency that is used
51+
// both by the crate and the compiler itself, we can't simply pass -L flags
52+
// as we'll get a duplicate matching versions. Instead, disambiguate with
53+
// `--extern dep=path`.
54+
// See https://github.com/rust-lang/rust-clippy/issues/4015.
55+
let needs_disambiguation = ["serde"];
56+
// This assumes that deps are compiled (they are for Cargo integration tests).
57+
let deps = std::fs::read_dir(host_libs().join("deps")).unwrap();
58+
let disambiguated = deps
59+
.filter_map(|dep| {
60+
let path = dep.ok()?.path();
61+
let name = path.file_name()?.to_string_lossy();
62+
// NOTE: This only handles a single dep
63+
// https://github.com/laumann/compiletest-rs/issues/101
64+
needs_disambiguation
65+
.iter()
66+
.find(|dep| name.starts_with(&format!("lib{}-", dep)))
67+
.map(|dep| format!("--extern {}={}", dep, path.display()))
68+
})
69+
.collect::<Vec<_>>();
70+
4971
config.target_rustcflags = Some(format!(
50-
"-L {0} -L {0}/deps -Dwarnings -Zui-testing",
51-
host_libs().display()
72+
"-L {0} -L {0}/deps -Dwarnings -Zui-testing {1}",
73+
host_libs().display(),
74+
disambiguated.join(" ")
5275
));
5376

5477
config.mode = cfg_mode;

tests/ui/methods.rs

Lines changed: 18 additions & 0 deletions
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)