Skip to content

Commit 598d7aa

Browse files
committed
Change HasSession to HasSourceMap
1 parent 91d7c57 commit 598d7aa

File tree

11 files changed

+125
-116
lines changed

11 files changed

+125
-116
lines changed

clippy_lints/src/bool_to_int_with_if.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::source::HasSession;
32
use clippy_utils::sugg::Sugg;
43
use clippy_utils::{higher, is_else_clause, is_in_const_context, span_contains_comment};
54
use rustc_ast::LitKind;
@@ -59,7 +58,7 @@ impl<'tcx> LateLintPass<'tcx> for BoolToIntWithIf {
5958
&& !is_in_const_context(cx)
6059
{
6160
let ty = cx.typeck_results().expr_ty(then);
62-
let mut applicability = if span_contains_comment(cx.sess().source_map(), expr.span) {
61+
let mut applicability = if span_contains_comment(cx, expr.span) {
6362
Applicability::MaybeIncorrect
6463
} else {
6564
Applicability::MachineApplicable

clippy_lints/src/double_parens.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::{HasSession, SpanRangeExt, snippet_with_applicability, snippet_with_context};
2+
use clippy_utils::source::{SpanRangeExt, snippet_with_applicability, snippet_with_context};
33
use rustc_ast::ast::{Expr, ExprKind, MethodCall};
44
use rustc_errors::Applicability;
5-
use rustc_lint::{EarlyContext, EarlyLintPass};
5+
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
66
use rustc_session::declare_lint_pass;
77

88
declare_clippy_lint! {

clippy_lints/src/if_not_else.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::consts::is_zero_integer_const;
22
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
33
use clippy_utils::is_else_clause;
4-
use clippy_utils::source::{HasSession, indent_of, reindent_multiline, snippet};
4+
use clippy_utils::source::{indent_of, reindent_multiline, snippet};
55
use rustc_errors::Applicability;
66
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
77
use rustc_lint::{LateContext, LateLintPass};
@@ -95,30 +95,30 @@ impl LateLintPass<'_> for IfNotElse {
9595
}
9696
}
9797

98-
fn make_sugg<'a>(
99-
sess: &impl HasSession,
100-
cond_kind: &'a ExprKind<'a>,
98+
fn make_sugg(
99+
cx: &LateContext<'_>,
100+
cond_kind: &ExprKind<'_>,
101101
cond_inner: Span,
102102
els_span: Span,
103-
default: &'a str,
103+
default: &str,
104104
indent_relative_to: Option<Span>,
105105
) -> String {
106-
let cond_inner_snip = snippet(sess, cond_inner, default);
107-
let els_snip = snippet(sess, els_span, default);
108-
let indent = indent_relative_to.and_then(|s| indent_of(sess, s));
106+
let cond_inner_snip = snippet(cx, cond_inner, default);
107+
let els_snip = snippet(cx, els_span, default);
108+
let indent = indent_relative_to.and_then(|s| indent_of(cx, s));
109109

110110
let suggestion = match cond_kind {
111111
ExprKind::Unary(UnOp::Not, cond_rest) => {
112112
format!(
113113
"if {} {} else {}",
114-
snippet(sess, cond_rest.span, default),
114+
snippet(cx, cond_rest.span, default),
115115
els_snip,
116116
cond_inner_snip
117117
)
118118
},
119119
ExprKind::Binary(_, lhs, rhs) => {
120-
let lhs_snip = snippet(sess, lhs.span, default);
121-
let rhs_snip = snippet(sess, rhs.span, default);
120+
let lhs_snip = snippet(cx, lhs.span, default);
121+
let rhs_snip = snippet(cx, rhs.span, default);
122122

123123
format!("if {lhs_snip} == {rhs_snip} {els_snip} else {cond_inner_snip}")
124124
},

clippy_lints/src/loops/manual_flatten.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::utils::make_iterator_snippet;
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::msrvs::{self, Msrv};
55
use clippy_utils::res::MaybeResPath;
6-
use clippy_utils::source::{HasSession, indent_of, reindent_multiline, snippet_with_applicability};
6+
use clippy_utils::source::{indent_of, reindent_multiline, snippet_with_applicability};
77
use clippy_utils::visitors::is_local_used;
88
use clippy_utils::{higher, is_refutable, peel_blocks_with_stmt, span_contains_comment};
99
use rustc_errors::Applicability;
@@ -50,7 +50,7 @@ pub(super) fn check<'tcx>(
5050
format!("unnecessary `if let` since only the `{if_let_type}` variant of the iterator element is used");
5151

5252
// Prepare the help message
53-
let mut applicability = if span_contains_comment(cx.sess().source_map(), body.span) {
53+
let mut applicability = if span_contains_comment(cx.tcx.sess.source_map(), body.span) {
5454
Applicability::MaybeIncorrect
5555
} else {
5656
Applicability::MachineApplicable

clippy_lints/src/loops/manual_slice_fill.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
33
use clippy_utils::msrvs::{self, Msrv};
4-
use clippy_utils::source::{HasSession, snippet_with_applicability};
4+
use clippy_utils::source::snippet_with_applicability;
55
use clippy_utils::ty::{implements_trait, is_slice_like};
66
use clippy_utils::visitors::is_local_used;
77
use clippy_utils::{higher, peel_blocks_with_stmt, span_contains_comment};
@@ -94,7 +94,7 @@ fn sugg<'tcx>(
9494
slice_span: rustc_span::Span,
9595
assignval_span: rustc_span::Span,
9696
) {
97-
let mut app = if span_contains_comment(cx.sess().source_map(), body.span) {
97+
let mut app = if span_contains_comment(cx, body.span) {
9898
Applicability::MaybeIncorrect // Comments may be informational.
9999
} else {
100100
Applicability::MachineApplicable

clippy_lints/src/manual_abs_diff.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::higher::If;
44
use clippy_utils::msrvs::{self, Msrv};
55
use clippy_utils::res::MaybeDef;
6-
use clippy_utils::source::HasSession as _;
76
use clippy_utils::sugg::Sugg;
87
use clippy_utils::ty::peel_and_count_ty_refs;
98
use clippy_utils::{eq_expr_value, peel_blocks, span_contains_comment};
@@ -77,10 +76,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualAbsDiff {
7776
(a, b) = (b, a);
7877
}
7978
let applicability = {
80-
let source_map = cx.sess().source_map();
81-
if span_contains_comment(source_map, if_expr.then.span)
82-
|| span_contains_comment(source_map, r#else.span)
83-
{
79+
if span_contains_comment(cx, if_expr.then.span) || span_contains_comment(cx, r#else.span) {
8480
Applicability::MaybeIncorrect
8581
} else {
8682
Applicability::MachineApplicable

clippy_lints/src/redundant_pub_crate.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::source::HasSession;
32
use rustc_errors::Applicability;
43
use rustc_hir::def::{DefKind, Res};
54
use rustc_hir::{Item, ItemKind, UseKind};
@@ -49,7 +48,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
4948
&& !cx.effective_visibilities.is_exported(item.owner_id.def_id)
5049
&& self.is_exported.last() == Some(&false)
5150
&& !is_ignorable_export(item)
52-
&& !item.span.in_external_macro(cx.sess().source_map())
51+
&& !item.span.in_external_macro(cx.tcx.sess.source_map())
5352
{
5453
let span = item
5554
.kind

clippy_lints/src/transmute/missing_transmute_annotations.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
22

33
use clippy_utils::diagnostics::span_lint_and_then;
4-
use clippy_utils::source::{HasSession, SpanRangeExt as _};
4+
use clippy_utils::source::SpanRangeExt as _;
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, GenericArg, HirId, LetStmt, Node, Path, TyKind};
77
use rustc_lint::LateContext;
@@ -111,8 +111,8 @@ fn ty_cannot_be_named(ty: Ty<'_>) -> bool {
111111
)
112112
}
113113

114-
fn maybe_name_by_expr<'a>(sess: &impl HasSession, span: Span, default: &'a str) -> Cow<'a, str> {
115-
span.with_source_text(sess, |name| {
114+
fn maybe_name_by_expr<'a>(cx: &LateContext<'_>, span: Span, default: &'a str) -> Cow<'a, str> {
115+
span.with_source_text(cx, |name| {
116116
(name.len() + 9 < default.len()).then_some(format!("`{name}`'s type").into())
117117
})
118118
.flatten()

clippy_utils/src/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
900900
if let Some(expr_span) = walk_span_to_context(expr.span, span.ctxt)
901901
&& let expr_lo = expr_span.lo()
902902
&& expr_lo >= span.lo
903-
&& let Some(src) = (span.lo..expr_lo).get_source_range(&self.tcx)
903+
&& let Some(src) = (span.lo..expr_lo).get_source_range(self.tcx)
904904
&& let Some(src) = src.as_str()
905905
{
906906
use rustc_lexer::TokenKind::{BlockComment, LineComment, OpenBrace, Semi, Whitespace};

clippy_utils/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ use crate::consts::{ConstEvalCtxt, Constant};
133133
use crate::higher::Range;
134134
use crate::msrvs::Msrv;
135135
use crate::res::{MaybeDef, MaybeQPath, MaybeResPath};
136+
use crate::source::HasSourceMap;
136137
use crate::ty::{adt_and_variant_of_res, can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type};
137138
use crate::visitors::for_each_expr_without_closures;
138139

@@ -2745,8 +2746,8 @@ pub fn tokenize_with_text(s: &str) -> impl Iterator<Item = (TokenKind, &str, Inn
27452746

27462747
/// Checks whether a given span has any comment token
27472748
/// This checks for all types of comment: line "//", block "/**", doc "///" "//!"
2748-
pub fn span_contains_comment(sm: &SourceMap, span: Span) -> bool {
2749-
let Ok(snippet) = sm.span_to_snippet(span) else {
2749+
pub fn span_contains_comment<'sm>(sm: impl HasSourceMap<'sm>, span: Span) -> bool {
2750+
let Ok(snippet) = sm.source_map().span_to_snippet(span) else {
27502751
return false;
27512752
};
27522753
return tokenize(&snippet, FrontmatterAllowed::No).any(|token| {
@@ -2761,7 +2762,7 @@ pub fn span_contains_comment(sm: &SourceMap, span: Span) -> bool {
27612762
/// token, including comments unless `skip_comments` is set.
27622763
/// This is useful to determine if there are any actual code tokens in the span that are omitted in
27632764
/// the late pass, such as platform-specific code.
2764-
pub fn span_contains_non_whitespace(cx: &impl source::HasSession, span: Span, skip_comments: bool) -> bool {
2765+
pub fn span_contains_non_whitespace<'sm>(cx: impl HasSourceMap<'sm>, span: Span, skip_comments: bool) -> bool {
27652766
matches!(span.get_source_text(cx), Some(snippet) if tokenize_with_text(&snippet).any(|(token, _, _)|
27662767
match token {
27672768
TokenKind::Whitespace => false,
@@ -2770,6 +2771,7 @@ pub fn span_contains_non_whitespace(cx: &impl source::HasSession, span: Span, sk
27702771
}
27712772
))
27722773
}
2774+
27732775
/// Returns all the comments a given span contains
27742776
///
27752777
/// Comments are returned wrapped with their relevant delimiters

0 commit comments

Comments
 (0)