Skip to content

Commit 9055f34

Browse files
committed
Merge remote-tracking branch 'upstream/master' into only_used_in_recursion
# Conflicts: # clippy_lints/src/lib.rs
2 parents 4f96ca3 + 5707491 commit 9055f34

34 files changed

+202
-141
lines changed

clippy_lints/src/dbg_macro.rs

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
2-
use clippy_utils::source::snippet_opt;
3-
use rustc_ast::ast;
4-
use rustc_ast::tokenstream::TokenStream;
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::macros::root_macro_call_first_node;
3+
use clippy_utils::source::snippet_with_applicability;
54
use rustc_errors::Applicability;
6-
use rustc_lint::{EarlyContext, EarlyLintPass};
5+
use rustc_hir::{Expr, ExprKind};
6+
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
8-
use rustc_span::source_map::Span;
8+
use rustc_span::sym;
99

1010
declare_clippy_lint! {
1111
/// ### What it does
@@ -15,14 +15,6 @@ declare_clippy_lint! {
1515
/// `dbg!` macro is intended as a debugging tool. It
1616
/// should not be in version control.
1717
///
18-
/// ### Known problems
19-
/// * The lint level is unaffected by crate attributes. The level can still
20-
/// be set for functions, modules and other items. To change the level for
21-
/// the entire crate, please use command line flags. More information and a
22-
/// configuration example can be found in [clippy#6610].
23-
///
24-
/// [clippy#6610]: https://github.com/rust-lang/rust-clippy/issues/6610#issuecomment-977120558
25-
///
2618
/// ### Example
2719
/// ```rust,ignore
2820
/// // Bad
@@ -39,37 +31,52 @@ declare_clippy_lint! {
3931

4032
declare_lint_pass!(DbgMacro => [DBG_MACRO]);
4133

42-
impl EarlyLintPass for DbgMacro {
43-
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::MacCall) {
44-
if mac.path == sym!(dbg) {
45-
if let Some(sugg) = tts_span(mac.args.inner_tokens()).and_then(|span| snippet_opt(cx, span)) {
46-
span_lint_and_sugg(
47-
cx,
48-
DBG_MACRO,
49-
mac.span(),
50-
"`dbg!` macro is intended as a debugging tool",
51-
"ensure to avoid having uses of it in version control",
52-
sugg,
53-
Applicability::MaybeIncorrect,
54-
);
55-
} else {
56-
span_lint_and_help(
57-
cx,
58-
DBG_MACRO,
59-
mac.span(),
60-
"`dbg!` macro is intended as a debugging tool",
61-
None,
62-
"ensure to avoid having uses of it in version control",
63-
);
64-
}
34+
impl LateLintPass<'_> for DbgMacro {
35+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
36+
let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return };
37+
if cx.tcx.is_diagnostic_item(sym::dbg_macro, macro_call.def_id) {
38+
let mut applicability = Applicability::MachineApplicable;
39+
let suggestion = match expr.peel_drop_temps().kind {
40+
// dbg!()
41+
ExprKind::Block(_, _) => String::new(),
42+
// dbg!(1)
43+
ExprKind::Match(val, ..) => {
44+
snippet_with_applicability(cx, val.span.source_callsite(), "..", &mut applicability).to_string()
45+
},
46+
// dbg!(2, 3)
47+
ExprKind::Tup(
48+
[
49+
Expr {
50+
kind: ExprKind::Match(first, ..),
51+
..
52+
},
53+
..,
54+
Expr {
55+
kind: ExprKind::Match(last, ..),
56+
..
57+
},
58+
],
59+
) => {
60+
let snippet = snippet_with_applicability(
61+
cx,
62+
first.span.source_callsite().to(last.span.source_callsite()),
63+
"..",
64+
&mut applicability,
65+
);
66+
format!("({snippet})")
67+
},
68+
_ => return,
69+
};
70+
71+
span_lint_and_sugg(
72+
cx,
73+
DBG_MACRO,
74+
macro_call.span,
75+
"`dbg!` macro is intended as a debugging tool",
76+
"ensure to avoid having uses of it in version control",
77+
suggestion,
78+
applicability,
79+
);
6580
}
6681
}
6782
}
68-
69-
// Get span enclosing entire the token stream.
70-
fn tts_span(tts: TokenStream) -> Option<Span> {
71-
let mut cursor = tts.into_trees();
72-
let first = cursor.next()?.span();
73-
let span = cursor.last().map_or(first, |tree| first.to(tree.span()));
74-
Some(span)
75-
}

clippy_lints/src/dereference.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::{
1212
};
1313
use rustc_lint::{LateContext, LateLintPass};
1414
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
15-
use rustc_middle::ty::{self, Ty, TyCtxt, TyS, TypeckResults};
15+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeckResults};
1616
use rustc_session::{declare_tool_lint, impl_lint_pass};
1717
use rustc_span::{symbol::sym, Span};
1818

@@ -448,7 +448,7 @@ fn try_parse_ref_op<'tcx>(
448448
// the reference.
449449
fn deref_method_same_type(result_ty: Ty<'_>, arg_ty: Ty<'_>) -> bool {
450450
match (result_ty.kind(), arg_ty.kind()) {
451-
(ty::Ref(_, result_ty, _), ty::Ref(_, arg_ty, _)) => TyS::same_type(result_ty, arg_ty),
451+
(ty::Ref(_, result_ty, _), ty::Ref(_, arg_ty, _)) => result_ty == arg_ty,
452452

453453
// The result type for a deref method is always a reference
454454
// Not matching the previous pattern means the argument type is not a reference

clippy_lints/src/implicit_hasher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::{Body, Expr, ExprKind, GenericArg, Item, ItemKind, QPath, TyKind}
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_middle::hir::nested_filter;
1010
use rustc_middle::lint::in_external_macro;
11-
use rustc_middle::ty::{Ty, TyS, TypeckResults};
11+
use rustc_middle::ty::{Ty, TypeckResults};
1212
use rustc_session::{declare_lint_pass, declare_tool_lint};
1313
use rustc_span::source_map::Span;
1414
use rustc_span::symbol::sym;
@@ -345,7 +345,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
345345
if let TyKind::Path(QPath::Resolved(None, ty_path)) = ty.kind;
346346
if let Some(ty_did) = ty_path.res.opt_def_id();
347347
then {
348-
if !TyS::same_type(self.target.ty(), self.maybe_typeck_results.unwrap().expr_ty(e)) {
348+
if self.target.ty() != self.maybe_typeck_results.unwrap().expr_ty(e) {
349349
return;
350350
}
351351

clippy_lints/src/index_refutable_slice.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::higher::IfLet;
44
use clippy_utils::ty::is_copy;
55
use clippy_utils::{is_expn_of, is_lint_allowed, meets_msrv, msrvs, path_to_local};
66
use if_chain::if_chain;
7-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7+
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
88
use rustc_errors::Applicability;
99
use rustc_hir as hir;
1010
use rustc_hir::intravisit::{self, Visitor};
@@ -92,9 +92,9 @@ impl<'tcx> LateLintPass<'tcx> for IndexRefutableSlice {
9292
extract_msrv_attr!(LateContext);
9393
}
9494

95-
fn find_slice_values(cx: &LateContext<'_>, pat: &hir::Pat<'_>) -> FxHashMap<hir::HirId, SliceLintInformation> {
95+
fn find_slice_values(cx: &LateContext<'_>, pat: &hir::Pat<'_>) -> FxIndexMap<hir::HirId, SliceLintInformation> {
9696
let mut removed_pat: FxHashSet<hir::HirId> = FxHashSet::default();
97-
let mut slices: FxHashMap<hir::HirId, SliceLintInformation> = FxHashMap::default();
97+
let mut slices: FxIndexMap<hir::HirId, SliceLintInformation> = FxIndexMap::default();
9898
pat.walk_always(|pat| {
9999
if let hir::PatKind::Binding(binding, value_hir_id, ident, sub_pat) = pat.kind {
100100
// We'll just ignore mut and ref mut for simplicity sake right now
@@ -208,10 +208,10 @@ impl SliceLintInformation {
208208

209209
fn filter_lintable_slices<'a, 'tcx>(
210210
cx: &'a LateContext<'tcx>,
211-
slice_lint_info: FxHashMap<hir::HirId, SliceLintInformation>,
211+
slice_lint_info: FxIndexMap<hir::HirId, SliceLintInformation>,
212212
max_suggested_slice: u64,
213213
scope: &'tcx hir::Expr<'tcx>,
214-
) -> FxHashMap<hir::HirId, SliceLintInformation> {
214+
) -> FxIndexMap<hir::HirId, SliceLintInformation> {
215215
let mut visitor = SliceIndexLintingVisitor {
216216
cx,
217217
slice_lint_info,
@@ -225,7 +225,7 @@ fn filter_lintable_slices<'a, 'tcx>(
225225

226226
struct SliceIndexLintingVisitor<'a, 'tcx> {
227227
cx: &'a LateContext<'tcx>,
228-
slice_lint_info: FxHashMap<hir::HirId, SliceLintInformation>,
228+
slice_lint_info: FxIndexMap<hir::HirId, SliceLintInformation>,
229229
max_suggested_slice: u64,
230230
}
231231

clippy_lints/src/len_zero.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::{
1010
ItemKind, Mutability, Node, TraitItemRef, TyKind,
1111
};
1212
use rustc_lint::{LateContext, LateLintPass};
13-
use rustc_middle::ty::{self, AssocKind, FnSig, Ty, TyS};
13+
use rustc_middle::ty::{self, AssocKind, FnSig, Ty};
1414
use rustc_session::{declare_lint_pass, declare_tool_lint};
1515
use rustc_span::{
1616
source_map::{Span, Spanned, Symbol},
@@ -265,7 +265,7 @@ impl LenOutput<'_> {
265265
(_, &ty::Bool) => true,
266266
(Self::Option(id), &ty::Adt(adt, subs)) if id == adt.did => subs.type_at(0).is_bool(),
267267
(Self::Result(id, err_ty), &ty::Adt(adt, subs)) if id == adt.did => {
268-
subs.type_at(0).is_bool() && TyS::same_type(subs.type_at(1), err_ty)
268+
subs.type_at(0).is_bool() && subs.type_at(1) == err_ty
269269
},
270270
_ => false,
271271
}

clippy_lints/src/lib.register_all.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
278278
LintId::of(transmute::TRANSMUTE_INT_TO_FLOAT),
279279
LintId::of(transmute::TRANSMUTE_NUM_TO_BYTES),
280280
LintId::of(transmute::TRANSMUTE_PTR_TO_REF),
281-
LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR),
282281
LintId::of(transmute::UNSOUND_COLLECTION_TRANSMUTE),
283282
LintId::of(transmute::WRONG_TRANSMUTE),
284283
LintId::of(transmuting_null::TRANSMUTING_NULL),

clippy_lints/src/lib.register_correctness.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
5858
LintId::of(size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT),
5959
LintId::of(swap::ALMOST_SWAPPED),
6060
LintId::of(to_string_in_display::TO_STRING_IN_DISPLAY),
61-
LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR),
6261
LintId::of(transmute::UNSOUND_COLLECTION_TRANSMUTE),
6362
LintId::of(transmute::WRONG_TRANSMUTE),
6463
LintId::of(transmuting_null::TRANSMUTING_NULL),

clippy_lints/src/lib.register_nursery.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
2626
LintId::of(strings::STRING_LIT_AS_BYTES),
2727
LintId::of(suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS),
2828
LintId::of(trailing_empty_array::TRAILING_EMPTY_ARRAY),
29+
LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR),
2930
LintId::of(transmute::USELESS_TRANSMUTE),
3031
LintId::of(use_self::USE_SELF),
3132
])

clippy_lints/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#![warn(rust_2018_idioms, unused_lifetimes)]
1919
// warn on rustc internal lints
2020
#![warn(rustc::internal)]
21+
// Disable this rustc lint for now, as it was also done in rustc
22+
#![allow(rustc::potential_query_instability)]
2123

2224
// FIXME: switch to something more ergonomic here, once available.
2325
// (Currently there is no way to opt into sysroot crates without `extern crate`.)
@@ -431,7 +433,6 @@ pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore, sess: &Se
431433

432434
store.register_pre_expansion_pass(|| Box::new(write::Write::default()));
433435
store.register_pre_expansion_pass(move || Box::new(attrs::EarlyAttributes { msrv }));
434-
store.register_pre_expansion_pass(|| Box::new(dbg_macro::DbgMacro));
435436
}
436437

437438
#[doc(hidden)]
@@ -864,6 +865,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
864865
store.register_late_pass(move || Box::new(manual_bits::ManualBits::new(msrv)));
865866
store.register_late_pass(|| Box::new(default_union_representation::DefaultUnionRepresentation));
866867
store.register_late_pass(|| Box::new(only_used_in_recursion::OnlyUsedInRecursion));
868+
store.register_late_pass(|| Box::new(dbg_macro::DbgMacro));
867869
// add lints here, do not remove this comment, it's used in `new_lint`
868870
}
869871

clippy_lints/src/loops/explicit_into_iter_loop.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ use clippy_utils::source::snippet_with_applicability;
55
use rustc_errors::Applicability;
66
use rustc_hir::Expr;
77
use rustc_lint::LateContext;
8-
use rustc_middle::ty::TyS;
98
use rustc_span::symbol::sym;
109

1110
pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<'_>) {
1211
let self_ty = cx.typeck_results().expr_ty(self_arg);
1312
let self_ty_adjusted = cx.typeck_results().expr_ty_adjusted(self_arg);
14-
if !(TyS::same_type(self_ty, self_ty_adjusted) && is_trait_method(cx, call_expr, sym::IntoIterator)) {
13+
if !(self_ty == self_ty_adjusted && is_trait_method(cx, call_expr, sym::IntoIterator)) {
1514
return;
1615
}
1716

0 commit comments

Comments
 (0)