Skip to content

Commit e3eede7

Browse files
committed
Merge remote-tracking branch 'upstream/master' into rustup
2 parents 611b74e + 7c9da3c commit e3eede7

File tree

65 files changed

+1182
-627
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1182
-627
lines changed

.github/workflows/clippy_bors.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@ jobs:
9090
- name: Checkout
9191
uses: actions/checkout@v2.3.3
9292

93-
# FIXME: should not be necessary once 1.24.2 is the default version on the windows runner
94-
- name: Update rustup
95-
run: rustup self update
96-
if: runner.os == 'Windows'
97-
9893
- name: Install toolchain
9994
run: rustup show active-toolchain
10095

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,6 +2295,7 @@ Released 2018-09-13
22952295
<!-- begin autogenerated links to lint list -->
22962296
[`absurd_extreme_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons
22972297
[`almost_swapped`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_swapped
2298+
[`append_instead_of_extend`]: https://rust-lang.github.io/rust-clippy/master/index.html#append_instead_of_extend
22982299
[`approx_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
22992300
[`as_conversions`]: https://rust-lang.github.io/rust-clippy/master/index.html#as_conversions
23002301
[`assertions_on_constants`]: https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants
@@ -2358,6 +2359,7 @@ Released 2018-09-13
23582359
[`derive_hash_xor_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#derive_hash_xor_eq
23592360
[`derive_ord_xor_partial_ord`]: https://rust-lang.github.io/rust-clippy/master/index.html#derive_ord_xor_partial_ord
23602361
[`disallowed_method`]: https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_method
2362+
[`disallowed_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#disallowed_type
23612363
[`diverging_sub_expression`]: https://rust-lang.github.io/rust-clippy/master/index.html#diverging_sub_expression
23622364
[`doc_markdown`]: https://rust-lang.github.io/rust-clippy/master/index.html#doc_markdown
23632365
[`double_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#double_comparisons

clippy_lints/src/bytecount.rs

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::ty::match_type;
4-
use clippy_utils::{contains_name, get_pat_name, paths, single_segment_path};
4+
use clippy_utils::visitors::LocalUsedVisitor;
5+
use clippy_utils::{path_to_local_id, paths, peel_ref_operators, remove_blocks, strip_pat_refs};
56
use if_chain::if_chain;
67
use rustc_errors::Applicability;
7-
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, UnOp};
8+
use rustc_hir::{BinOpKind, Expr, ExprKind, PatKind};
89
use rustc_lint::{LateContext, LateLintPass};
910
use rustc_middle::ty::{self, UintTy};
1011
use rustc_session::{declare_lint_pass, declare_tool_lint};
1112
use rustc_span::sym;
12-
use rustc_span::Symbol;
1313

1414
declare_clippy_lint! {
1515
/// **What it does:** Checks for naive byte counts
@@ -38,42 +38,43 @@ declare_lint_pass!(ByteCount => [NAIVE_BYTECOUNT]);
3838
impl<'tcx> LateLintPass<'tcx> for ByteCount {
3939
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
4040
if_chain! {
41-
if let ExprKind::MethodCall(count, _, count_args, _) = expr.kind;
41+
if let ExprKind::MethodCall(count, _, [count_recv], _) = expr.kind;
4242
if count.ident.name == sym!(count);
43-
if count_args.len() == 1;
44-
if let ExprKind::MethodCall(filter, _, filter_args, _) = count_args[0].kind;
43+
if let ExprKind::MethodCall(filter, _, [filter_recv, filter_arg], _) = count_recv.kind;
4544
if filter.ident.name == sym!(filter);
46-
if filter_args.len() == 2;
47-
if let ExprKind::Closure(_, _, body_id, _, _) = filter_args[1].kind;
45+
if let ExprKind::Closure(_, _, body_id, _, _) = filter_arg.kind;
4846
let body = cx.tcx.hir().body(body_id);
49-
if body.params.len() == 1;
50-
if let Some(argname) = get_pat_name(body.params[0].pat);
47+
if let [param] = body.params;
48+
if let PatKind::Binding(_, arg_id, _, _) = strip_pat_refs(param.pat).kind;
5149
if let ExprKind::Binary(ref op, l, r) = body.value.kind;
5250
if op.node == BinOpKind::Eq;
5351
if match_type(cx,
54-
cx.typeck_results().expr_ty(&filter_args[0]).peel_refs(),
52+
cx.typeck_results().expr_ty(filter_recv).peel_refs(),
5553
&paths::SLICE_ITER);
54+
let operand_is_arg = |expr| {
55+
let expr = peel_ref_operators(cx, remove_blocks(expr));
56+
path_to_local_id(expr, arg_id)
57+
};
58+
let needle = if operand_is_arg(l) {
59+
r
60+
} else if operand_is_arg(r) {
61+
l
62+
} else {
63+
return;
64+
};
65+
if ty::Uint(UintTy::U8) == *cx.typeck_results().expr_ty(needle).peel_refs().kind();
66+
if !LocalUsedVisitor::new(cx, arg_id).check_expr(needle);
5667
then {
57-
let needle = match get_path_name(l) {
58-
Some(name) if check_arg(name, argname, r) => r,
59-
_ => match get_path_name(r) {
60-
Some(name) if check_arg(name, argname, l) => l,
61-
_ => { return; }
62-
}
63-
};
64-
if ty::Uint(UintTy::U8) != *cx.typeck_results().expr_ty(needle).peel_refs().kind() {
65-
return;
66-
}
6768
let haystack = if let ExprKind::MethodCall(path, _, args, _) =
68-
filter_args[0].kind {
69+
filter_recv.kind {
6970
let p = path.ident.name;
7071
if (p == sym::iter || p == sym!(iter_mut)) && args.len() == 1 {
7172
&args[0]
7273
} else {
73-
&filter_args[0]
74+
&filter_recv
7475
}
7576
} else {
76-
&filter_args[0]
77+
&filter_recv
7778
};
7879
let mut applicability = Applicability::MaybeIncorrect;
7980
span_lint_and_sugg(
@@ -91,24 +92,3 @@ impl<'tcx> LateLintPass<'tcx> for ByteCount {
9192
};
9293
}
9394
}
94-
95-
fn check_arg(name: Symbol, arg: Symbol, needle: &Expr<'_>) -> bool {
96-
name == arg && !contains_name(name, needle)
97-
}
98-
99-
fn get_path_name(expr: &Expr<'_>) -> Option<Symbol> {
100-
match expr.kind {
101-
ExprKind::Box(e) | ExprKind::AddrOf(BorrowKind::Ref, _, e) | ExprKind::Unary(UnOp::Deref, e) => {
102-
get_path_name(e)
103-
},
104-
ExprKind::Block(b, _) => {
105-
if b.stmts.is_empty() {
106-
b.expr.as_ref().and_then(|p| get_path_name(p))
107-
} else {
108-
None
109-
}
110-
},
111-
ExprKind::Path(ref qpath) => single_segment_path(qpath).map(|ps| ps.ident.name),
112-
_ => None,
113-
}
114-
}

clippy_lints/src/collapsible_match.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::visitors::LocalUsedVisitor;
3-
use clippy_utils::{is_lang_ctor, path_to_local, SpanlessEq};
3+
use clippy_utils::{is_lang_ctor, path_to_local, peel_ref_operators, SpanlessEq};
44
use if_chain::if_chain;
55
use rustc_hir::LangItem::OptionNone;
6-
use rustc_hir::{Arm, Expr, ExprKind, Guard, HirId, Pat, PatKind, StmtKind, UnOp};
6+
use rustc_hir::{Arm, Expr, ExprKind, Guard, HirId, Pat, PatKind, StmtKind};
77
use rustc_lint::{LateContext, LateLintPass};
8-
use rustc_middle::ty::TypeckResults;
98
use rustc_session::{declare_lint_pass, declare_tool_lint};
109
use rustc_span::{MultiSpan, Span};
1110

@@ -73,7 +72,7 @@ fn check_arm<'tcx>(arm: &Arm<'tcx>, wild_outer_arm: &Arm<'tcx>, cx: &LateContext
7372
if arms_inner.iter().all(|arm| arm.guard.is_none());
7473
// match expression must be a local binding
7574
// match <local> { .. }
76-
if let Some(binding_id) = path_to_local(strip_ref_operators(expr_in, cx.typeck_results()));
75+
if let Some(binding_id) = path_to_local(peel_ref_operators(cx, expr_in));
7776
// one of the branches must be "wild-like"
7877
if let Some(wild_inner_arm_idx) = arms_inner.iter().rposition(|arm_inner| arm_is_wild_like(cx, arm_inner));
7978
let (wild_inner_arm, non_wild_inner_arm) =
@@ -163,16 +162,3 @@ fn pat_contains_or(pat: &Pat<'_>) -> bool {
163162
});
164163
result
165164
}
166-
167-
/// Removes `AddrOf` operators (`&`) or deref operators (`*`), but only if a reference type is
168-
/// dereferenced. An overloaded deref such as `Vec` to slice would not be removed.
169-
fn strip_ref_operators<'hir>(mut expr: &'hir Expr<'hir>, typeck_results: &TypeckResults<'_>) -> &'hir Expr<'hir> {
170-
loop {
171-
match expr.kind {
172-
ExprKind::AddrOf(_, _, e) => expr = e,
173-
ExprKind::Unary(UnOp::Deref, e) if typeck_results.expr_ty(e).is_ref() => expr = e,
174-
_ => break,
175-
}
176-
}
177-
expr
178-
}

clippy_lints/src/default.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_errors::Applicability;
77
use rustc_hir::def::Res;
88
use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
99
use rustc_lint::{LateContext, LateLintPass};
10-
use rustc_middle::lint::in_external_macro;
1110
use rustc_middle::ty;
1211
use rustc_session::{declare_tool_lint, impl_lint_pass};
1312
use rustc_span::symbol::{Ident, Symbol};
@@ -122,7 +121,7 @@ impl LateLintPass<'_> for Default {
122121
if let StmtKind::Local(local) = stmt.kind;
123122
if let Some(expr) = local.init;
124123
if !any_parent_is_automatically_derived(cx.tcx, expr.hir_id);
125-
if !in_external_macro(cx.tcx.sess, expr.span);
124+
if !in_macro(expr.span);
126125
// only take bindings to identifiers
127126
if let PatKind::Binding(_, binding_id, ident, _) = local.pat.kind;
128127
// only when assigning `... = Default::default()`

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use rustc_hir::{
77
intravisit::{walk_expr, walk_stmt, NestedVisitorMap, Visitor},
88
Body, Expr, ExprKind, HirId, Lit, Stmt, StmtKind,
99
};
10-
use rustc_lint::{LateContext, LateLintPass};
10+
use rustc_lint::{LateContext, LateLintPass, LintContext};
1111
use rustc_middle::{
1212
hir::map::Map,
13+
lint::in_external_macro,
1314
ty::{self, FloatTy, IntTy, PolyFnSig, Ty},
1415
};
1516
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -73,6 +74,7 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
7374
/// Check whether a passed literal has potential to cause fallback or not.
7475
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>) {
7576
if_chain! {
77+
if !in_external_macro(self.cx.sess(), lit.span);
7678
if let Some(ty_bound) = self.ty_bounds.last();
7779
if matches!(lit.node,
7880
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));

clippy_lints/src/disallowed_method.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::fn_def_id;
33

44
use rustc_data_structures::fx::FxHashSet;
5-
use rustc_hir::Expr;
5+
use rustc_hir::{def::Res, def_id::DefId, Crate, Expr};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_tool_lint, impl_lint_pass};
88
use rustc_span::Symbol;
@@ -13,21 +13,14 @@ declare_clippy_lint! {
1313
/// **Why is this bad?** Some methods are undesirable in certain contexts,
1414
/// and it's beneficial to lint for them as needed.
1515
///
16-
/// **Known problems:** Currently, you must write each function as a
17-
/// fully-qualified path. This lint doesn't support aliases or reexported
18-
/// names; be aware that many types in `std` are actually reexports.
19-
///
20-
/// For example, if you want to disallow `Duration::as_secs`, your clippy.toml
21-
/// configuration would look like
22-
/// `disallowed-methods = ["core::time::Duration::as_secs"]` and not
23-
/// `disallowed-methods = ["std::time::Duration::as_secs"]` as you might expect.
16+
/// **Known problems:** None.
2417
///
2518
/// **Example:**
2619
///
2720
/// An example clippy.toml configuration:
2821
/// ```toml
2922
/// # clippy.toml
30-
/// disallowed-methods = ["alloc::vec::Vec::leak", "std::time::Instant::now"]
23+
/// disallowed-methods = ["std::vec::Vec::leak", "std::time::Instant::now"]
3124
/// ```
3225
///
3326
/// ```rust,ignore
@@ -52,6 +45,7 @@ declare_clippy_lint! {
5245
#[derive(Clone, Debug)]
5346
pub struct DisallowedMethod {
5447
disallowed: FxHashSet<Vec<Symbol>>,
48+
def_ids: FxHashSet<(DefId, Vec<Symbol>)>,
5549
}
5650

5751
impl DisallowedMethod {
@@ -61,17 +55,28 @@ impl DisallowedMethod {
6155
.iter()
6256
.map(|s| s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>())
6357
.collect(),
58+
def_ids: FxHashSet::default(),
6459
}
6560
}
6661
}
6762

6863
impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
6964

7065
impl<'tcx> LateLintPass<'tcx> for DisallowedMethod {
66+
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
67+
for path in &self.disallowed {
68+
let segs = path.iter().map(ToString::to_string).collect::<Vec<_>>();
69+
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &segs.iter().map(String::as_str).collect::<Vec<_>>())
70+
{
71+
self.def_ids.insert((id, path.clone()));
72+
}
73+
}
74+
}
75+
7176
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
7277
if let Some(def_id) = fn_def_id(cx, expr) {
73-
let func_path = cx.get_def_path(def_id);
74-
if self.disallowed.contains(&func_path) {
78+
if self.def_ids.iter().any(|(id, _)| def_id == *id) {
79+
let func_path = cx.get_def_path(def_id);
7580
let func_path_string = func_path
7681
.into_iter()
7782
.map(Symbol::to_ident_string)

0 commit comments

Comments
 (0)