Skip to content

Commit 1984752

Browse files
committed
Auto merge of #13255 - Jarcho:get_src_display2, r=Manishearth
Remove more `snippet_opt` calls First commit is the same as #13244 changelog: none
2 parents 52192aa + ddf2ba5 commit 1984752

23 files changed

+161
-118
lines changed

clippy_lints/src/attrs/empty_line_after.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{EMPTY_LINE_AFTER_DOC_COMMENTS, EMPTY_LINE_AFTER_OUTER_ATTR};
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::source::{is_present_in_source, snippet_opt, without_block_comments};
3+
use clippy_utils::source::{is_present_in_source, without_block_comments, SpanRangeExt};
44
use rustc_ast::{AttrKind, AttrStyle};
55
use rustc_lint::EarlyContext;
66
use rustc_span::Span;
@@ -26,7 +26,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, item: &rustc_ast::Item) {
2626
item.span.parent(),
2727
);
2828

29-
if let Some(snippet) = snippet_opt(cx, end_of_attr_to_next_attr_or_item) {
29+
if let Some(snippet) = end_of_attr_to_next_attr_or_item.get_source_text(cx) {
3030
let lines = snippet.split('\n').collect::<Vec<_>>();
3131
let lines = without_block_comments(lines);
3232

clippy_lints/src/attrs/non_minimal_cfg.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{Attribute, NON_MINIMAL_CFG};
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::source::SpanRangeExt;
44
use rustc_ast::{MetaItemKind, NestedMetaItem};
55
use rustc_errors::Applicability;
66
use rustc_lint::EarlyContext;
@@ -29,8 +29,13 @@ fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
2929
meta.span,
3030
"unneeded sub `cfg` when there is only one condition",
3131
|diag| {
32-
if let Some(snippet) = snippet_opt(cx, list[0].span()) {
33-
diag.span_suggestion(meta.span, "try", snippet, Applicability::MaybeIncorrect);
32+
if let Some(snippet) = list[0].span().get_source_text(cx) {
33+
diag.span_suggestion(
34+
meta.span,
35+
"try",
36+
snippet.to_owned(),
37+
Applicability::MaybeIncorrect,
38+
);
3439
}
3540
},
3641
);

clippy_lints/src/attrs/unnecessary_clippy_cfg.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{Attribute, UNNECESSARY_CLIPPY_CFG};
22
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg};
3-
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::source::SpanRangeExt;
4+
use itertools::Itertools;
45
use rustc_ast::AttrStyle;
56
use rustc_errors::Applicability;
67
use rustc_lint::{EarlyContext, Level};
@@ -31,7 +32,7 @@ pub(super) fn check(
3132
return;
3233
}
3334
if nb_items == clippy_lints.len() {
34-
if let Some(snippet) = snippet_opt(cx, behind_cfg_attr.span) {
35+
if let Some(snippet) = behind_cfg_attr.span.get_source_text(cx) {
3536
span_lint_and_sugg(
3637
cx,
3738
UNNECESSARY_CLIPPY_CFG,
@@ -47,11 +48,7 @@ pub(super) fn check(
4748
);
4849
}
4950
} else {
50-
let snippet = clippy_lints
51-
.iter()
52-
.filter_map(|sp| snippet_opt(cx, *sp))
53-
.collect::<Vec<_>>()
54-
.join(",");
51+
let snippet = clippy_lints.iter().filter_map(|sp| sp.get_source_text(cx)).join(",");
5552
span_lint_and_note(
5653
cx,
5754
UNNECESSARY_CLIPPY_CFG,

clippy_lints/src/attrs/useless_attribute.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::utils::{extract_clippy_lint, is_lint_level, is_word};
22
use super::{Attribute, USELESS_ATTRIBUTE};
33
use clippy_utils::diagnostics::span_lint_and_then;
4-
use clippy_utils::source::{first_line_of_span, snippet_opt};
4+
use clippy_utils::source::{first_line_of_span, SpanRangeExt};
55
use rustc_ast::NestedMetaItem;
66
use rustc_errors::Applicability;
77
use rustc_hir::{Item, ItemKind};
@@ -69,14 +69,14 @@ pub(super) fn check(cx: &LateContext<'_>, item: &Item<'_>, attrs: &[Attribute])
6969
}
7070
let line_span = first_line_of_span(cx, attr.span);
7171

72-
if let Some(mut sugg) = snippet_opt(cx, line_span) {
73-
if sugg.contains("#[") {
72+
if let Some(src) = line_span.get_source_text(cx) {
73+
if src.contains("#[") {
74+
#[expect(clippy::collapsible_span_lint_calls)]
7475
span_lint_and_then(cx, USELESS_ATTRIBUTE, line_span, "useless lint attribute", |diag| {
75-
sugg = sugg.replacen("#[", "#![", 1);
7676
diag.span_suggestion(
7777
line_span,
7878
"if you just forgot a `!`, use",
79-
sugg,
79+
src.replacen("#[", "#![", 1),
8080
Applicability::MaybeIncorrect,
8181
);
8282
});

clippy_lints/src/casts/as_ptr_cast_mut.rs

+2-2
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_errors::Applicability;
44
use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::LateContext;
@@ -19,7 +19,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
1919
&& let as_ptr_sig = cx.tcx.fn_sig(as_ptr_did).instantiate_identity()
2020
&& let Some(first_param_ty) = as_ptr_sig.skip_binder().inputs().iter().next()
2121
&& let ty::Ref(_, _, Mutability::Not) = first_param_ty.kind()
22-
&& let Some(recv) = snippet_opt(cx, receiver.span)
22+
&& let Some(recv) = receiver.span.get_source_text(cx)
2323
{
2424
// `as_mut_ptr` might not exist
2525
let applicability = Applicability::MaybeIncorrect;

clippy_lints/src/manual_async_fn.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::source::{position_before_rarrow, snippet_block, snippet_opt};
2+
use clippy_utils::source::{position_before_rarrow, snippet_block, SpanRangeExt};
33
use rustc_errors::Applicability;
44
use rustc_hir::intravisit::FnKind;
55
use rustc_hir::{
@@ -68,8 +68,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn {
6868
header_span,
6969
"this function can be simplified using the `async fn` syntax",
7070
|diag| {
71-
if let Some(vis_snip) = snippet_opt(cx, *vis_span)
72-
&& let Some(header_snip) = snippet_opt(cx, header_span)
71+
if let Some(vis_snip) = vis_span.get_source_text(cx)
72+
&& let Some(header_snip) = header_span.get_source_text(cx)
7373
&& let Some(ret_pos) = position_before_rarrow(&header_snip)
7474
&& let Some((ret_sugg, ret_snip)) = suggested_ret(cx, output)
7575
{
@@ -190,6 +190,6 @@ fn suggested_ret(cx: &LateContext<'_>, output: &Ty<'_>) -> Option<(&'static str,
190190
Some((sugg, String::new()))
191191
} else {
192192
let sugg = "return the output of the future directly";
193-
snippet_opt(cx, output.span).map(|snip| (sugg, format!(" -> {snip}")))
193+
output.span.get_source_text(cx).map(|src| (sugg, format!(" -> {src}")))
194194
}
195195
}

clippy_lints/src/matches/single_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::{MATCH_BOOL, SINGLE_MATCH, SINGLE_MATCH_ELSE};
2222
/// span, e.g. a string literal `"//"`, but we know that this isn't the case for empty
2323
/// match arms.
2424
fn empty_arm_has_comment(cx: &LateContext<'_>, span: Span) -> bool {
25-
if let Some(ff) = span.get_source_text(cx)
25+
if let Some(ff) = span.get_source_range(cx)
2626
&& let Some(text) = ff.as_str()
2727
{
2828
text.as_bytes().windows(2).any(|w| w == b"//" || w == b"/*")

clippy_lints/src/needless_pass_by_value.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_self;
33
use clippy_utils::ptr::get_spans;
4-
use clippy_utils::source::{snippet, snippet_opt};
4+
use clippy_utils::source::{snippet, SpanRangeExt};
55
use clippy_utils::ty::{
66
implements_trait, implements_trait_with_env_from_iter, is_copy, is_type_diagnostic_item, is_type_lang_item,
77
};
@@ -242,8 +242,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
242242
for (span, suggestion) in clone_spans {
243243
diag.span_suggestion(
244244
span,
245-
snippet_opt(cx, span)
246-
.map_or("change the call to".into(), |x| format!("change `{x}` to")),
245+
span.get_source_text(cx)
246+
.map_or("change the call to".to_owned(), |src| format!("change `{src}` to")),
247247
suggestion,
248248
Applicability::Unspecified,
249249
);
@@ -267,8 +267,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
267267
for (span, suggestion) in clone_spans {
268268
diag.span_suggestion(
269269
span,
270-
snippet_opt(cx, span)
271-
.map_or("change the call to".into(), |x| format!("change `{x}` to")),
270+
span.get_source_text(cx)
271+
.map_or("change the call to".to_owned(), |src| format!("change `{src}` to")),
272272
suggestion,
273273
Applicability::Unspecified,
274274
);

clippy_lints/src/pathbuf_init_then_push.rs

+4-4
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::path_to_local_id;
3-
use clippy_utils::source::{snippet, snippet_opt};
3+
use clippy_utils::source::{snippet, SpanRangeExt};
44
use clippy_utils::ty::is_type_diagnostic_item;
55
use rustc_ast::{LitKind, StrStyle};
66
use rustc_errors::Applicability;
@@ -74,7 +74,7 @@ impl<'tcx> PathbufPushSearcher<'tcx> {
7474
&& let Some(arg) = self.arg
7575
&& let ExprKind::Lit(x) = arg.kind
7676
&& let LitKind::Str(_, StrStyle::Cooked) = x.node
77-
&& let Some(s) = snippet_opt(cx, arg.span)
77+
&& let Some(s) = arg.span.get_source_text(cx)
7878
{
7979
Some(format!(" = PathBuf::from({s});"))
8080
} else {
@@ -84,8 +84,8 @@ impl<'tcx> PathbufPushSearcher<'tcx> {
8484

8585
fn gen_pathbuf_join(&self, cx: &LateContext<'_>) -> Option<String> {
8686
let arg = self.arg?;
87-
let arg_str = snippet_opt(cx, arg.span)?;
88-
let init_val = snippet_opt(cx, self.init_val.span)?;
87+
let arg_str = arg.span.get_source_text(cx)?;
88+
let init_val = self.init_val.span.get_source_text(cx)?;
8989
Some(format!(" = {init_val}.join({arg_str});"))
9090
}
9191

clippy_lints/src/ptr.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Checks for usage of `&Vec[_]` and `&String`.
22
33
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
4-
use clippy_utils::source::snippet_opt;
4+
use clippy_utils::source::SpanRangeExt;
55
use clippy_utils::ty::expr_sig;
66
use clippy_utils::visitors::contains_unsafe_block;
77
use clippy_utils::{get_expr_use_or_unification_node, is_lint_allowed, path_def_id, path_to_local};
@@ -243,7 +243,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
243243
.chain(result.replacements.iter().map(|r| {
244244
(
245245
r.expr_span,
246-
format!("{}{}", snippet_opt(cx, r.self_span).unwrap(), r.replacement),
246+
format!("{}{}", r.self_span.get_source_text(cx).unwrap(), r.replacement),
247247
)
248248
}))
249249
.collect(),
@@ -372,7 +372,7 @@ impl fmt::Display for DerefTyDisplay<'_, '_> {
372372
DerefTy::Path => f.write_str("Path"),
373373
DerefTy::Slice(hir_ty, ty) => {
374374
f.write_char('[')?;
375-
match hir_ty.and_then(|s| snippet_opt(self.0, s)) {
375+
match hir_ty.and_then(|s| s.get_source_text(self.0)) {
376376
Some(s) => f.write_str(&s)?,
377377
None => ty.fmt(f)?,
378378
}
@@ -413,6 +413,7 @@ impl<'tcx> DerefTy<'tcx> {
413413
}
414414
}
415415

416+
#[expect(clippy::too_many_lines)]
416417
fn check_fn_args<'cx, 'tcx: 'cx>(
417418
cx: &'cx LateContext<'tcx>,
418419
fn_sig: ty::FnSig<'tcx>,
@@ -488,8 +489,6 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
488489
return None;
489490
}
490491

491-
let ty_name = snippet_opt(cx, ty.span()).unwrap_or_else(|| args.type_at(1).to_string());
492-
493492
span_lint_hir_and_then(
494493
cx,
495494
PTR_ARG,
@@ -500,7 +499,10 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
500499
diag.span_suggestion(
501500
hir_ty.span,
502501
"change this to",
503-
format!("&{}{ty_name}", mutability.prefix_str()),
502+
match ty.span().get_source_text(cx) {
503+
Some(s) => format!("&{}{s}", mutability.prefix_str()),
504+
None => format!("&{}{}", mutability.prefix_str(), args.type_at(1)),
505+
},
504506
Applicability::Unspecified,
505507
);
506508
},

clippy_lints/src/ptr_offset_with_cast.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
2-
use clippy_utils::source::snippet_opt;
2+
use clippy_utils::source::SpanRangeExt;
33
use rustc_errors::Applicability;
44
use rustc_hir::{Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
@@ -120,8 +120,8 @@ fn build_suggestion(
120120
receiver_expr: &Expr<'_>,
121121
cast_lhs_expr: &Expr<'_>,
122122
) -> Option<String> {
123-
let receiver = snippet_opt(cx, receiver_expr.span)?;
124-
let cast_lhs = snippet_opt(cx, cast_lhs_expr.span)?;
123+
let receiver = receiver_expr.span.get_source_text(cx)?;
124+
let cast_lhs = cast_lhs_expr.span.get_source_text(cx)?;
125125
Some(format!("{receiver}.{}({cast_lhs})", method.suggestion()))
126126
}
127127

clippy_lints/src/redundant_clone.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint_hir, span_lint_hir_and_then};
22
use clippy_utils::mir::{visit_local_usage, LocalUsage, PossibleBorrowerMap};
3-
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::source::SpanRangeExt;
44
use clippy_utils::ty::{has_drop, is_copy, is_type_diagnostic_item, is_type_lang_item, walk_ptrs_ty_depth};
55
use clippy_utils::{fn_has_unsatisfiable_preds, match_def_path, paths};
66
use rustc_errors::Applicability;
@@ -208,7 +208,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
208208
.assert_crate_local()
209209
.lint_root;
210210

211-
if let Some(snip) = snippet_opt(cx, span)
211+
if let Some(snip) = span.get_source_text(cx)
212212
&& let Some(dot) = snip.rfind('.')
213213
{
214214
let sugg_span = span.with_lo(span.lo() + BytePos(u32::try_from(dot).unwrap()));

clippy_lints/src/reference.rs

+5-5
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::{snippet_opt, snippet_with_applicability};
2+
use clippy_utils::source::{snippet_with_applicability, SpanRangeExt};
33
use rustc_ast::ast::{Expr, ExprKind, Mutability, UnOp};
44
use rustc_errors::Applicability;
55
use rustc_lint::{EarlyContext, EarlyLintPass};
66
use rustc_session::declare_lint_pass;
7-
use rustc_span::BytePos;
7+
use rustc_span::{BytePos, Span};
88

99
declare_clippy_lint! {
1010
/// ### What it does
@@ -56,11 +56,11 @@ impl EarlyLintPass for DerefAddrOf {
5656
{
5757
let mut applicability = Applicability::MachineApplicable;
5858
let sugg = if e.span.from_expansion() {
59-
if let Some(macro_source) = snippet_opt(cx, e.span) {
59+
if let Some(macro_source) = e.span.get_source_text(cx) {
6060
// Remove leading whitespace from the given span
6161
// e.g: ` $visitor` turns into `$visitor`
62-
let trim_leading_whitespaces = |span| {
63-
snippet_opt(cx, span)
62+
let trim_leading_whitespaces = |span: Span| {
63+
span.get_source_text(cx)
6464
.and_then(|snip| {
6565
#[expect(clippy::cast_possible_truncation)]
6666
snip.find(|c: char| !c.is_whitespace())

clippy_lints/src/regex.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt::Display;
22

33
use clippy_utils::consts::{ConstEvalCtxt, Constant};
44
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
5-
use clippy_utils::source::snippet_opt;
5+
use clippy_utils::source::SpanRangeExt;
66
use clippy_utils::{def_path_def_ids, path_def_id, paths};
77
use rustc_ast::ast::{LitKind, StrStyle};
88
use rustc_hir::def_id::DefIdMap;
@@ -122,7 +122,7 @@ fn lint_syntax_error(cx: &LateContext<'_>, error: &regex_syntax::Error, unescape
122122
};
123123

124124
if let Some((primary, auxiliary, kind)) = parts
125-
&& let Some(literal_snippet) = snippet_opt(cx, base)
125+
&& let Some(literal_snippet) = base.get_source_text(cx)
126126
&& let Some(inner) = literal_snippet.get(offset as usize..)
127127
// Only convert to native rustc spans if the parsed regex matches the
128128
// source snippet exactly, to ensure the span offsets are correct

clippy_lints/src/returns.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
2-
use clippy_utils::source::{snippet_opt, snippet_with_context};
2+
use clippy_utils::source::{snippet_with_context, SpanRangeExt};
33
use clippy_utils::sugg::has_enclosing_paren;
44
use clippy_utils::visitors::{for_each_expr, Descend};
55
use clippy_utils::{
@@ -250,20 +250,25 @@ impl<'tcx> LateLintPass<'tcx> for Return {
250250
|err| {
251251
err.span_label(local.span, "unnecessary `let` binding");
252252

253-
if let Some(mut snippet) = snippet_opt(cx, initexpr.span) {
254-
if binary_expr_needs_parentheses(initexpr) {
255-
if !has_enclosing_paren(&snippet) {
256-
snippet = format!("({snippet})");
253+
if let Some(src) = initexpr.span.get_source_text(cx) {
254+
let sugg = if binary_expr_needs_parentheses(initexpr) {
255+
if has_enclosing_paren(&src) {
256+
src.to_owned()
257+
} else {
258+
format!("({src})")
257259
}
258260
} else if !cx.typeck_results().expr_adjustments(retexpr).is_empty() {
259-
if !has_enclosing_paren(&snippet) {
260-
snippet = format!("({snippet})");
261+
if has_enclosing_paren(&src) {
262+
format!("{src} as _")
263+
} else {
264+
format!("({src}) as _")
261265
}
262-
snippet.push_str(" as _");
263-
}
266+
} else {
267+
src.to_owned()
268+
};
264269
err.multipart_suggestion(
265270
"return the expression directly",
266-
vec![(local.span, String::new()), (retexpr.span, snippet)],
271+
vec![(local.span, String::new()), (retexpr.span, sugg)],
267272
Applicability::MachineApplicable,
268273
);
269274
} else {

0 commit comments

Comments
 (0)