Skip to content

Commit 3274ed9

Browse files
committed
Change HasSession to HasSourceMap
1 parent 2cf2931 commit 3274ed9

File tree

8 files changed

+104
-99
lines changed

8 files changed

+104
-99
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/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};
@@ -91,30 +91,30 @@ impl LateLintPass<'_> for IfNotElse {
9191
}
9292
}
9393

94-
fn make_sugg<'a>(
95-
sess: &impl HasSession,
96-
cond_kind: &'a ExprKind<'a>,
94+
fn make_sugg(
95+
cx: &LateContext<'_>,
96+
cond_kind: &ExprKind<'_>,
9797
cond_inner: Span,
9898
els_span: Span,
99-
default: &'a str,
99+
default: &str,
100100
indent_relative_to: Option<Span>,
101101
) -> String {
102-
let cond_inner_snip = snippet(sess, cond_inner, default);
103-
let els_snip = snippet(sess, els_span, default);
104-
let indent = indent_relative_to.and_then(|s| indent_of(sess, s));
102+
let cond_inner_snip = snippet(cx, cond_inner, default);
103+
let els_snip = snippet(cx, els_span, default);
104+
let indent = indent_relative_to.and_then(|s| indent_of(cx, s));
105105

106106
let suggestion = match cond_kind {
107107
ExprKind::Unary(UnOp::Not, cond_rest) => {
108108
format!(
109109
"if {} {} else {}",
110-
snippet(sess, cond_rest.span, default),
110+
snippet(cx, cond_rest.span, default),
111111
els_snip,
112112
cond_inner_snip
113113
)
114114
},
115115
ExprKind::Binary(_, lhs, rhs) => {
116-
let lhs_snip = snippet(sess, lhs.span, default);
117-
let rhs_snip = snippet(sess, rhs.span, default);
116+
let lhs_snip = snippet(cx, lhs.span, default);
117+
let rhs_snip = snippet(cx, rhs.span, default);
118118

119119
format!("if {lhs_snip} == {rhs_snip} {els_snip} else {cond_inner_snip}")
120120
},

clippy_lints/src/loops/manual_flatten.rs

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

5151
// Prepare the help message
52-
let mut applicability = if span_contains_comment(cx.sess().source_map(), body.span) {
52+
let mut applicability = if span_contains_comment(cx.tcx.sess.source_map(), body.span) {
5353
Applicability::MaybeIncorrect
5454
} else {
5555
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};
@@ -93,7 +93,7 @@ fn sugg<'tcx>(
9393
slice_span: rustc_span::Span,
9494
assignval_span: rustc_span::Span,
9595
) {
96-
let mut app = if span_contains_comment(cx.sess().source_map(), body.span) {
96+
let mut app = if span_contains_comment(cx, body.span) {
9797
Applicability::MaybeIncorrect // Comments may be informational.
9898
} else {
9999
Applicability::MachineApplicable

clippy_lints/src/manual_abs_diff.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::higher::If;
44
use clippy_utils::msrvs::{self, Msrv};
5-
use clippy_utils::source::HasSession as _;
65
use clippy_utils::sugg::Sugg;
76
use clippy_utils::ty::{is_type_diagnostic_item, peel_and_count_ty_refs};
87
use clippy_utils::{eq_expr_value, peel_blocks, span_contains_comment};
@@ -76,10 +75,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualAbsDiff {
7675
(a, b) = (b, a);
7776
}
7877
let applicability = {
79-
let source_map = cx.sess().source_map();
80-
if span_contains_comment(source_map, if_expr.then.span)
81-
|| span_contains_comment(source_map, r#else.span)
82-
{
78+
if span_contains_comment(cx, if_expr.then.span) || span_contains_comment(cx, r#else.span) {
8379
Applicability::MaybeIncorrect
8480
} else {
8581
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_utils/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ use visitors::{Visitable, for_each_unconsumed_temporary};
129129
use crate::ast_utils::unordered_over;
130130
use crate::consts::{ConstEvalCtxt, Constant, mir_to_const};
131131
use crate::higher::Range;
132+
use crate::source::HasSourceMap;
132133
use crate::ty::{adt_and_variant_of_res, can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type};
133134
use crate::visitors::for_each_expr_without_closures;
134135

@@ -2855,8 +2856,8 @@ pub fn tokenize_with_text(s: &str) -> impl Iterator<Item = (TokenKind, &str, Inn
28552856

28562857
/// Checks whether a given span has any comment token
28572858
/// This checks for all types of comment: line "//", block "/**", doc "///" "//!"
2858-
pub fn span_contains_comment(sm: &SourceMap, span: Span) -> bool {
2859-
let Ok(snippet) = sm.span_to_snippet(span) else {
2859+
pub fn span_contains_comment(sm: &impl HasSourceMap, span: Span) -> bool {
2860+
let Ok(snippet) = sm.source_map().span_to_snippet(span) else {
28602861
return false;
28612862
};
28622863
return tokenize(&snippet, FrontmatterAllowed::No).any(|token| {
@@ -2871,7 +2872,7 @@ pub fn span_contains_comment(sm: &SourceMap, span: Span) -> bool {
28712872
/// token, including comments unless `skip_comments` is set.
28722873
/// This is useful to determine if there are any actual code tokens in the span that are omitted in
28732874
/// the late pass, such as platform-specific code.
2874-
pub fn span_contains_non_whitespace(cx: &impl source::HasSession, span: Span, skip_comments: bool) -> bool {
2875+
pub fn span_contains_non_whitespace(cx: &impl HasSourceMap, span: Span, skip_comments: bool) -> bool {
28752876
matches!(span.get_source_text(cx), Some(snippet) if tokenize_with_text(&snippet).any(|(token, _, _)|
28762877
match token {
28772878
TokenKind::Whitespace => false,

0 commit comments

Comments
 (0)