Skip to content

Commit 14d2aa1

Browse files
committed
Auto merge of #7542 - LeSeulArtichaut:cleanups, r=flip1995
Cleanup usage of `span_to_snippet` and `LintContext::sess` - avoid using `SourceMap::span_to_snippet` directly and use `clippy_utils::source::snippet_opt` instead - don't use `LintContext::sess()` on `EarlyContext`s which have a `sess` field directly available, saving the import of `LintContext` changelog: none
2 parents 0084195 + ae700b4 commit 14d2aa1

11 files changed

+36
-34
lines changed

clippy_lints/src/as_conversions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use rustc_ast::ast::{Expr, ExprKind};
3-
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
3+
use rustc_lint::{EarlyContext, EarlyLintPass};
44
use rustc_middle::lint::in_external_macro;
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
66

@@ -47,7 +47,7 @@ declare_lint_pass!(AsConversions => [AS_CONVERSIONS]);
4747

4848
impl EarlyLintPass for AsConversions {
4949
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
50-
if in_external_macro(cx.sess(), expr.span) {
50+
if in_external_macro(cx.sess, expr.span) {
5151
return;
5252
}
5353

clippy_lints/src/else_if_without_else.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use clippy_utils::diagnostics::span_lint_and_help;
44
use rustc_ast::ast::{Expr, ExprKind};
5-
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
5+
use rustc_lint::{EarlyContext, EarlyLintPass};
66
use rustc_middle::lint::in_external_macro;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88

@@ -49,7 +49,7 @@ declare_lint_pass!(ElseIfWithoutElse => [ELSE_IF_WITHOUT_ELSE]);
4949

5050
impl EarlyLintPass for ElseIfWithoutElse {
5151
fn check_expr(&mut self, cx: &EarlyContext<'_>, mut item: &Expr) {
52-
if in_external_macro(cx.sess(), item.span) {
52+
if in_external_macro(cx.sess, item.span) {
5353
return;
5454
}
5555

clippy_lints/src/if_not_else.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use clippy_utils::diagnostics::span_lint_and_help;
55
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, UnOp};
6-
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
6+
use rustc_lint::{EarlyContext, EarlyLintPass};
77
use rustc_middle::lint::in_external_macro;
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99

@@ -48,7 +48,7 @@ declare_lint_pass!(IfNotElse => [IF_NOT_ELSE]);
4848

4949
impl EarlyLintPass for IfNotElse {
5050
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
51-
if in_external_macro(cx.sess(), item.span) {
51+
if in_external_macro(cx.sess, item.span) {
5252
return;
5353
}
5454
if let ExprKind::If(ref cond, _, Some(ref els)) = item.kind {

clippy_lints/src/items_after_statements.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use clippy_utils::diagnostics::span_lint;
44
use rustc_ast::ast::{Block, ItemKind, StmtKind};
5-
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
5+
use rustc_lint::{EarlyContext, EarlyLintPass};
66
use rustc_middle::lint::in_external_macro;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88

@@ -54,7 +54,7 @@ declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]);
5454

5555
impl EarlyLintPass for ItemsAfterStatements {
5656
fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
57-
if in_external_macro(cx.sess(), item.span) {
57+
if in_external_macro(cx.sess, item.span) {
5858
return;
5959
}
6060

@@ -68,7 +68,7 @@ impl EarlyLintPass for ItemsAfterStatements {
6868
// lint on all further items
6969
for stmt in stmts {
7070
if let StmtKind::Item(ref it) = *stmt {
71-
if in_external_macro(cx.sess(), it.span) {
71+
if in_external_macro(cx.sess, it.span) {
7272
return;
7373
}
7474
if let ItemKind::MacroDef(..) = it.kind {

clippy_lints/src/literal_representation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use clippy_utils::{
1010
use if_chain::if_chain;
1111
use rustc_ast::ast::{Expr, ExprKind, Lit, LitKind};
1212
use rustc_errors::Applicability;
13-
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
13+
use rustc_lint::{EarlyContext, EarlyLintPass};
1414
use rustc_middle::lint::in_external_macro;
1515
use rustc_session::{declare_tool_lint, impl_lint_pass};
1616
use std::iter;
@@ -222,7 +222,7 @@ impl_lint_pass!(LiteralDigitGrouping => [
222222

223223
impl EarlyLintPass for LiteralDigitGrouping {
224224
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
225-
if in_external_macro(cx.sess(), expr.span) {
225+
if in_external_macro(cx.sess, expr.span) {
226226
return;
227227
}
228228

@@ -415,7 +415,7 @@ impl_lint_pass!(DecimalLiteralRepresentation => [DECIMAL_LITERAL_REPRESENTATION]
415415

416416
impl EarlyLintPass for DecimalLiteralRepresentation {
417417
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
418-
if in_external_macro(cx.sess(), expr.span) {
418+
if in_external_macro(cx.sess, expr.span) {
419419
return;
420420
}
421421

clippy_lints/src/methods/from_iter_instead_of_collect.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet_opt;
23
use clippy_utils::ty::implements_trait;
34
use clippy_utils::{is_expr_path_def_path, paths, sugg};
45
use if_chain::if_chain;
56
use rustc_errors::Applicability;
67
use rustc_hir as hir;
7-
use rustc_lint::{LateContext, LintContext};
8+
use rustc_lint::LateContext;
89
use rustc_middle::ty::Ty;
910
use rustc_span::sym;
1011

@@ -43,7 +44,7 @@ fn extract_turbofish(cx: &LateContext<'_>, expr: &hir::Expr<'_>, ty: Ty<'tcx>) -
4344

4445
let call_site = expr.span.source_callsite();
4546
if_chain! {
46-
if let Ok(snippet) = cx.sess().source_map().span_to_snippet(call_site);
47+
if let Some(snippet) = snippet_opt(cx, call_site);
4748
let snippet_split = snippet.split("::").collect::<Vec<_>>();
4849
if let Some((_, elements)) = snippet_split.split_last();
4950

clippy_lints/src/misc_early/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use clippy_utils::source::snippet_opt;
1212
use rustc_ast::ast::{Expr, Generics, Lit, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind};
1313
use rustc_ast::visit::FnKind;
1414
use rustc_data_structures::fx::FxHashMap;
15-
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
15+
use rustc_lint::{EarlyContext, EarlyLintPass};
1616
use rustc_middle::lint::in_external_macro;
1717
use rustc_session::{declare_lint_pass, declare_tool_lint};
1818
use rustc_span::source_map::Span;
@@ -307,7 +307,7 @@ impl EarlyLintPass for MiscEarlyLints {
307307
}
308308

309309
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
310-
if in_external_macro(cx.sess(), expr.span) {
310+
if in_external_macro(cx.sess, expr.span) {
311311
return;
312312
}
313313
double_neg::check(cx, expr);

clippy_lints/src/misc_early/unneeded_field_pattern.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
2+
use clippy_utils::source::snippet_opt;
23
use rustc_ast::ast::{Pat, PatKind};
3-
use rustc_lint::{EarlyContext, LintContext};
4+
use rustc_lint::EarlyContext;
45

56
use super::UNNEEDED_FIELD_PATTERN;
67

@@ -48,7 +49,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
4849
match field.pat.kind {
4950
PatKind::Wild => {},
5051
_ => {
51-
if let Ok(n) = cx.sess().source_map().span_to_snippet(field.span) {
52+
if let Some(n) = snippet_opt(cx, field.span) {
5253
normal.push(n);
5354
}
5455
},

clippy_lints/src/redundant_closure_call.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_errors::Applicability;
88
use rustc_hir as hir;
99
use rustc_hir::intravisit as hir_visit;
1010
use rustc_hir::intravisit::Visitor as HirVisitor;
11-
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
11+
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
1212
use rustc_middle::hir::map::Map;
1313
use rustc_middle::lint::in_external_macro;
1414
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -63,7 +63,7 @@ impl<'ast> ast_visit::Visitor<'ast> for ReturnVisitor {
6363

6464
impl EarlyLintPass for RedundantClosureCall {
6565
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
66-
if in_external_macro(cx.sess(), expr.span) {
66+
if in_external_macro(cx.sess, expr.span) {
6767
return;
6868
}
6969
if_chain! {

clippy_lints/src/reference.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ impl EarlyLintPass for DerefAddrOf {
5454
then {
5555
let mut applicability = Applicability::MachineApplicable;
5656
let sugg = if e.span.from_expansion() {
57-
if let Ok(macro_source) = cx.sess.source_map().span_to_snippet(e.span) {
57+
#[allow(clippy::option_if_let_else)]
58+
if let Some(macro_source) = snippet_opt(cx, e.span) {
5859
// Remove leading whitespace from the given span
5960
// e.g: ` $visitor` turns into `$visitor`
6061
let trim_leading_whitespaces = |span| {

clippy_lints/src/unused_unit.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::position_before_rarrow;
2+
use clippy_utils::source::{position_before_rarrow, snippet_opt};
33
use if_chain::if_chain;
44
use rustc_ast::ast;
55
use rustc_ast::visit::FnKind;
66
use rustc_errors::Applicability;
7-
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
7+
use rustc_lint::{EarlyContext, EarlyLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99
use rustc_span::source_map::Span;
1010
use rustc_span::BytePos;
@@ -125,17 +125,16 @@ fn is_unit_expr(expr: &ast::Expr) -> bool {
125125
}
126126

127127
fn lint_unneeded_unit_return(cx: &EarlyContext<'_>, ty: &ast::Ty, span: Span) {
128-
let (ret_span, appl) = if let Ok(fn_source) = cx.sess().source_map().span_to_snippet(span.with_hi(ty.span.hi())) {
129-
position_before_rarrow(&fn_source).map_or((ty.span, Applicability::MaybeIncorrect), |rpos| {
130-
(
131-
#[allow(clippy::cast_possible_truncation)]
132-
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
133-
Applicability::MachineApplicable,
134-
)
135-
})
136-
} else {
137-
(ty.span, Applicability::MaybeIncorrect)
138-
};
128+
let (ret_span, appl) =
129+
snippet_opt(cx, span.with_hi(ty.span.hi())).map_or((ty.span, Applicability::MaybeIncorrect), |fn_source| {
130+
position_before_rarrow(&fn_source).map_or((ty.span, Applicability::MaybeIncorrect), |rpos| {
131+
(
132+
#[allow(clippy::cast_possible_truncation)]
133+
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
134+
Applicability::MachineApplicable,
135+
)
136+
})
137+
});
139138
span_lint_and_sugg(
140139
cx,
141140
UNUSED_UNIT,

0 commit comments

Comments
 (0)