Skip to content

Commit af475b7

Browse files
committed
Replace snippet_opt with SpanRangeExt::*_source_text
1 parent 382881f commit af475b7

18 files changed

+99
-97
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::numeric_literal::NumericLiteral;
3-
use clippy_utils::source::{SpanRangeExt, snippet_opt};
3+
use clippy_utils::source::SpanRangeExt;
44
use clippy_utils::visitors::{Visitable, for_each_expr_without_closures};
55
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias, path_to_local};
66
use rustc_ast::{LitFloatType, LitIntType, LitKind};
@@ -23,7 +23,8 @@ pub(super) fn check<'tcx>(
2323
cast_from: Ty<'tcx>,
2424
cast_to: Ty<'tcx>,
2525
) -> bool {
26-
let cast_str = snippet_opt(cx, cast_expr.span).unwrap_or_default();
26+
let cast_str = cast_expr.span.get_source_text(cx);
27+
let cast_str = cast_str.as_deref().unwrap_or("");
2728

2829
if let ty::RawPtr(..) = cast_from.kind()
2930
// check both mutability and type are the same
@@ -56,7 +57,7 @@ pub(super) fn check<'tcx>(
5657
"casting raw pointers to the same type and constness is unnecessary (`{cast_from}` -> `{cast_to}`)"
5758
),
5859
"try",
59-
cast_str.clone(),
60+
cast_str.to_string(),
6061
Applicability::MaybeIncorrect,
6162
);
6263
}
@@ -102,7 +103,7 @@ pub(super) fn check<'tcx>(
102103
}
103104

104105
if let Some(lit) = get_numeric_literal(cast_expr) {
105-
let literal_str = &cast_str;
106+
let literal_str = cast_str;
106107

107108
if let LitKind::Int(n, _) = lit.node
108109
&& let Some(src) = cast_expr.span.get_source_text(cx)
@@ -167,7 +168,7 @@ pub(super) fn check<'tcx>(
167168
sym::assert_ne_macro,
168169
sym::debug_assert_ne_macro,
169170
];
170-
matches!(expr.span.ctxt().outer_expn_data().macro_def_id, Some(def_id) if
171+
matches!(expr.span.ctxt().outer_expn_data().macro_def_id, Some(def_id) if
171172
cx.tcx.get_diagnostic_name(def_id).is_some_and(|sym| ALLOWED_MACROS.contains(&sym)))
172173
}
173174

@@ -198,7 +199,7 @@ pub(super) fn check<'tcx>(
198199
match surrounding {
199200
MaybeParenOrBlock::Paren => format!("({cast_str})"),
200201
MaybeParenOrBlock::Block => format!("{{ {cast_str} }}"),
201-
MaybeParenOrBlock::Nothing => cast_str,
202+
MaybeParenOrBlock::Nothing => cast_str.to_string(),
202203
},
203204
Applicability::MachineApplicable,
204205
);

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::numeric_literal;
3-
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::source::SpanRangeExt;
44
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
55
use rustc_errors::Applicability;
66
use rustc_hir::intravisit::{Visitor, walk_expr, walk_pat, walk_stmt};
@@ -11,6 +11,7 @@ use rustc_hir::{
1111
use rustc_lint::{LateContext, LateLintPass, LintContext};
1212
use rustc_middle::ty::{self, FloatTy, IntTy, PolyFnSig, Ty};
1313
use rustc_session::declare_lint_pass;
14+
use std::borrow::Cow;
1415
use std::iter;
1516

1617
declare_clippy_lint! {
@@ -103,12 +104,13 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
103104
lit.span,
104105
"default numeric fallback might occur",
105106
|diag| {
106-
let src = if let Some(src) = snippet_opt(self.cx, lit.span) {
107-
src
107+
let src = lit.span.get_source_text(self.cx);
108+
let src: Cow<'_, str> = if let Some(src) = src.as_deref() {
109+
src.into()
108110
} else {
109111
match lit.node {
110-
LitKind::Int(src, _) => format!("{src}"),
111-
LitKind::Float(src, _) => format!("{src}"),
112+
LitKind::Int(src, _) => format!("{src}").into(),
113+
LitKind::Float(src, _) => format!("{src}").into(),
112114
_ => unreachable!("Default numeric fallback never results in other types"),
113115
}
114116
};

clippy_lints/src/doc/include_in_doc_without_cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::snippet_opt;
2+
use clippy_utils::source::SpanRangeExt;
33
use rustc_ast::{AttrArgs, AttrKind, AttrStyle, Attribute};
44
use rustc_errors::Applicability;
55
use rustc_lint::EarlyContext;
@@ -15,7 +15,7 @@ pub fn check(cx: &EarlyContext<'_>, attrs: &[Attribute]) {
1515
&& !attr.span.contains(meta.span)
1616
// Since the `include_str` is already expanded at this point, we can only take the
1717
// whole attribute snippet and then modify for our suggestion.
18-
&& let Some(snippet) = snippet_opt(cx, attr.span)
18+
&& let Some(snippet) = attr.span.get_source_text(cx)
1919
// We cannot remove this because a `#[doc = include_str!("...")]` attribute can occupy
2020
// several lines.
2121
&& let Some(start) = snippet.find('[')

clippy_lints/src/doc/too_long_first_doc_paragraph.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_lint::LateContext;
55

66
use clippy_utils::diagnostics::span_lint_and_then;
77
use clippy_utils::is_from_proc_macro;
8-
use clippy_utils::source::snippet_opt;
8+
use clippy_utils::source::SpanRangeExt;
99

1010
use super::TOO_LONG_FIRST_DOC_PARAGRAPH;
1111

@@ -81,8 +81,8 @@ pub(super) fn check(
8181
if should_suggest_empty_doc
8282
&& let Some(second_span) = spans.get(1)
8383
&& let new_span = first_span.with_hi(second_span.lo()).with_lo(first_span.hi())
84-
&& let Some(snippet) = snippet_opt(cx, new_span)
85-
&& let Some(first) = snippet_opt(cx, *first_span)
84+
&& let Some(snippet) = new_span.get_source_text(cx)
85+
&& let Some(first) = first_span.get_source_text(cx)
8686
&& let Some(comment_form) = first.get(..3)
8787
{
8888
diag.span_suggestion(

clippy_lints/src/eta_reduction.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::higher::VecArgs;
3-
use clippy_utils::source::{snippet_opt, snippet_with_applicability};
3+
use clippy_utils::source::{SpanRangeExt, snippet_with_applicability};
44
use clippy_utils::ty::get_type_diagnostic_name;
55
use clippy_utils::usage::{local_used_after_expr, local_used_in};
66
use clippy_utils::{
@@ -217,8 +217,8 @@ fn check_closure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tcx
217217
expr.span,
218218
"redundant closure",
219219
|diag| {
220-
if let Some(mut snippet) = snippet_opt(cx, callee.span) {
221-
if path_to_local(callee).is_some_and(|l| {
220+
if let Some(snippet) = callee.span.get_source_text(cx) {
221+
let snippet = if path_to_local(callee).is_some_and(|l| {
222222
// FIXME: Do we really need this `local_used_in` check?
223223
// Isn't it checking something like... `callee(callee)`?
224224
// If somehow this check is needed, add some test for it,
@@ -227,11 +227,9 @@ fn check_closure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tcx
227227
}) {
228228
match closure_kind {
229229
// Mutable closure is used after current expr; we cannot consume it.
230-
ClosureKind::FnMut => snippet = format!("&mut {snippet}"),
231-
ClosureKind::Fn if !callee_ty_raw.is_ref() => {
232-
snippet = format!("&{snippet}");
233-
},
234-
_ => (),
230+
ClosureKind::FnMut => format!("&mut {snippet}"),
231+
ClosureKind::Fn if !callee_ty_raw.is_ref() => format!("&{snippet}"),
232+
_ => snippet.to_owned(),
235233
}
236234
} else {
237235
let n_refs = callee_ty_adjustments.iter().rev().fold(0, |acc, adjustment| {
@@ -242,9 +240,11 @@ fn check_closure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tcx
242240
}
243241
});
244242
if n_refs > 0 {
245-
snippet = format!("{}{snippet}", "*".repeat(n_refs));
243+
format!("{}{snippet}", "*".repeat(n_refs))
244+
} else {
245+
snippet.to_owned()
246246
}
247-
}
247+
};
248248

249249
let replace_with = match callee_ty_adjusted.kind() {
250250
ty::FnDef(def, _) => cx.tcx.def_descr(*def),

clippy_lints/src/formatting.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
22
use clippy_utils::is_span_if;
3-
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::source::SpanRangeExt;
44
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind};
55
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
66
use rustc_session::declare_lint_pass;
@@ -170,23 +170,21 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
170170
{
171171
let eq_span = lhs.span.between(rhs.span);
172172
if let ExprKind::Unary(op, ref sub_rhs) = rhs.kind
173-
&& let Some(eq_snippet) = snippet_opt(cx, eq_span)
173+
&& eq_span.check_source_text(cx, |eq_snippet| eq_snippet.ends_with('='))
174174
{
175175
let op = op.as_str();
176176
let eqop_span = lhs.span.between(sub_rhs.span);
177-
if eq_snippet.ends_with('=') {
178-
span_lint_and_note(
179-
cx,
180-
SUSPICIOUS_ASSIGNMENT_FORMATTING,
181-
eqop_span,
182-
format!(
183-
"this looks like you are trying to use `.. {op}= ..`, but you \
177+
span_lint_and_note(
178+
cx,
179+
SUSPICIOUS_ASSIGNMENT_FORMATTING,
180+
eqop_span,
181+
format!(
182+
"this looks like you are trying to use `.. {op}= ..`, but you \
184183
really are doing `.. = ({op} ..)`"
185-
),
186-
None,
187-
format!("to remove this lint, use either `{op}=` or `= {op}`"),
188-
);
189-
}
184+
),
185+
None,
186+
format!("to remove this lint, use either `{op}=` or `= {op}`"),
187+
);
190188
}
191189
}
192190
}
@@ -201,11 +199,11 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) {
201199
&& let ExprKind::Unary(op, ref un_rhs) = rhs.kind
202200
// from UnOp operator to UnOp operand
203201
&& let unop_operand_span = rhs.span.until(un_rhs.span)
204-
&& let Some(binop_snippet) = snippet_opt(cx, binop_span)
205-
&& let Some(unop_operand_snippet) = snippet_opt(cx, unop_operand_span)
206202
&& let binop_str = binop.node.as_str()
207-
// no space after BinOp operator and space after UnOp operator
208-
&& binop_snippet.ends_with(binop_str) && unop_operand_snippet.ends_with(' ')
203+
// no space after BinOp operator
204+
&& binop_span.check_source_text(cx, |binop_snippet| binop_snippet.ends_with(binop_str))
205+
// ... and space after UnOp operator
206+
&& unop_operand_span.check_source_text(cx, |unop_operand_snippet| unop_operand_snippet.ends_with(' '))
209207
{
210208
let unop_str = op.as_str();
211209
let eqop_span = lhs.span.between(un_rhs.span);
@@ -239,7 +237,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
239237

240238
// the snippet should look like " else \n " with maybe comments anywhere
241239
// it’s bad when there is a ‘\n’ after the “else”
242-
&& let Some(else_snippet) = snippet_opt(cx, else_span)
240+
&& let Some(else_snippet) = else_span.get_source_text(cx)
243241
&& let Some((pre_else, post_else)) = else_snippet.split_once("else")
244242
&& !else_snippet.contains('/')
245243
&& let Some((_, post_else_post_eol)) = post_else.split_once('\n')
@@ -293,8 +291,7 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
293291
&& has_unary_equivalent(op.node)
294292
&& lhs.span.eq_ctxt(op.span)
295293
&& let space_span = lhs.span.between(op.span)
296-
&& let Some(space_snippet) = snippet_opt(cx, space_span)
297-
&& space_snippet.contains('\n')
294+
&& space_span.check_source_text(cx, |space_snippet| space_snippet.contains('\n'))
298295
&& indentation(cx, op.span) <= indentation(cx, lhs.span)
299296
{
300297
let lint_span = lhs.span.shrink_to_hi();
@@ -322,8 +319,9 @@ fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
322319
// If there is a line break between the two expressions, don't lint.
323320
// If there is a non-whitespace character, this span came from a proc-macro.
324321
&& let else_span = first.span.between(second.span)
325-
&& let Some(else_snippet) = snippet_opt(cx, else_span)
326-
&& !else_snippet.chars().any(|c| c == '\n' || !c.is_whitespace())
322+
&& else_span.check_source_text(cx, |else_snippet| {
323+
!else_snippet.chars().any(|c| c == '\n' || !c.is_whitespace())
324+
})
327325
{
328326
let (looks_like, next_thing) = if is_if(second) {
329327
("an `else if`", "the second `if`")

clippy_lints/src/implicit_saturating_sub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ fn check_subtraction(
237237
{
238238
if eq_expr_value(cx, left, big_expr) && eq_expr_value(cx, right, little_expr) {
239239
// This part of the condition is voluntarily split from the one before to ensure that
240-
// if `snippet_opt` fails, it won't try the next conditions.
240+
// if `Sugg::hir_opt` fails, it won't try the next conditions.
241241
if (!is_in_const_context(cx) || msrv.meets(cx, msrvs::SATURATING_SUB_CONST))
242242
&& let Some(big_expr_sugg) = Sugg::hir_opt(cx, big_expr).map(Sugg::maybe_paren)
243243
&& let Some(little_expr_sugg) = Sugg::hir_opt(cx, little_expr)

clippy_lints/src/large_include_file.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::macros::root_macro_call_first_node;
4-
use clippy_utils::source::snippet_opt;
4+
use clippy_utils::source::SpanRangeExt;
55
use rustc_ast::{AttrArgs, AttrKind, Attribute, LitKind};
66
use rustc_hir::{Expr, ExprKind};
77
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
@@ -96,7 +96,7 @@ impl EarlyLintPass for LargeIncludeFile {
9696
&& !attr.span.contains(meta.span)
9797
// Since the `include_str` is already expanded at this point, we can only take the
9898
// whole attribute snippet and then modify for our suggestion.
99-
&& let Some(snippet) = snippet_opt(cx, attr.span)
99+
&& let Some(snippet) = attr.span.get_source_text(cx)
100100
// We cannot remove this because a `#[doc = include_str!("...")]` attribute can
101101
// occupy several lines.
102102
&& let Some(start) = snippet.find('[')

clippy_lints/src/loops/while_let_loop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::WHILE_LET_LOOP;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::source::{snippet, snippet_indent, snippet_opt};
3+
use clippy_utils::source::{SpanRangeExt, snippet, snippet_indent};
44
use clippy_utils::ty::needs_ordered_drop;
55
use clippy_utils::visitors::any_temporaries_need_ordered_drop;
66
use clippy_utils::{higher, peel_blocks};
@@ -89,8 +89,8 @@ fn could_be_while_let<'tcx>(
8989
// Prevent trivial reassignments such as `let x = x;` or `let _ = …;`, but
9090
// keep them if the type has been explicitly specified.
9191
&& (!is_trivial_assignment(pat, peel_blocks(inner_expr)) || ty.is_some())
92-
&& let Some(pat_str) = snippet_opt(cx, pat.span)
93-
&& let Some(init_str) = snippet_opt(cx, peel_blocks(inner_expr).span)
92+
&& let Some(pat_str) = pat.span.get_source_text(cx)
93+
&& let Some(init_str) = peel_blocks(inner_expr).span.get_source_text(cx)
9494
{
9595
let ty_str = ty
9696
.map(|ty| format!(": {}", snippet(cx, ty.span, "_")))

clippy_lints/src/manual_option_as_slice.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::msrvs::Msrv;
4+
use clippy_utils::source::SpanRangeExt;
45
use clippy_utils::{is_none_pattern, msrvs, peel_hir_expr_refs, sym};
56
use rustc_errors::Applicability;
67
use rustc_hir::def::{DefKind, Res};
@@ -140,7 +141,7 @@ fn check_as_ref(cx: &LateContext<'_>, expr: &Expr<'_>, span: Span, msrv: Msrv) {
140141
span,
141142
"manual implementation of `Option::as_slice`",
142143
|diag| {
143-
if let Some(snippet) = clippy_utils::source::snippet_opt(cx, callee.span) {
144+
if let Some(snippet) = callee.span.get_source_text(cx) {
144145
diag.span_suggestion(
145146
span,
146147
"use",

0 commit comments

Comments
 (0)