Skip to content

Commit ecfc7d9

Browse files
committed
Auto merge of rust-lang#13244 - Jarcho:get_src_display, r=Alexendoo
Start removing `snippet_opt` in favor of `get_source_text` Continuing the job of removing unnecessary allocations. changelog: none
2 parents a83146a + 8a4c34a commit ecfc7d9

29 files changed

+177
-190
lines changed

clippy_lints/src/booleans.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
22
use clippy_utils::eq_expr_value;
3-
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::source::SpanRangeExt;
44
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
55
use rustc_ast::ast::LitKind;
66
use rustc_errors::Applicability;
@@ -134,28 +134,30 @@ fn check_inverted_bool_in_condition(
134134

135135
let suggestion = match (left.kind, right.kind) {
136136
(ExprKind::Unary(UnOp::Not, left_sub), ExprKind::Unary(UnOp::Not, right_sub)) => {
137-
let Some(left) = snippet_opt(cx, left_sub.span) else {
137+
let Some(left) = left_sub.span.get_source_text(cx) else {
138138
return;
139139
};
140-
let Some(right) = snippet_opt(cx, right_sub.span) else {
140+
let Some(right) = right_sub.span.get_source_text(cx) else {
141141
return;
142142
};
143143
let Some(op) = bin_op_eq_str(op) else { return };
144144
format!("{left} {op} {right}")
145145
},
146146
(ExprKind::Unary(UnOp::Not, left_sub), _) => {
147-
let Some(left) = snippet_opt(cx, left_sub.span) else {
147+
let Some(left) = left_sub.span.get_source_text(cx) else {
148148
return;
149149
};
150-
let Some(right) = snippet_opt(cx, right.span) else {
150+
let Some(right) = right.span.get_source_text(cx) else {
151151
return;
152152
};
153153
let Some(op) = inverted_bin_op_eq_str(op) else { return };
154154
format!("{left} {op} {right}")
155155
},
156156
(_, ExprKind::Unary(UnOp::Not, right_sub)) => {
157-
let Some(left) = snippet_opt(cx, left.span) else { return };
158-
let Some(right) = snippet_opt(cx, right_sub.span) else {
157+
let Some(left) = left.span.get_source_text(cx) else {
158+
return;
159+
};
160+
let Some(right) = right_sub.span.get_source_text(cx) else {
159161
return;
160162
};
161163
let Some(op) = inverted_bin_op_eq_str(op) else { return };
@@ -313,8 +315,7 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
313315
self.output.push_str(&str);
314316
} else {
315317
self.output.push('!');
316-
let snip = snippet_opt(self.cx, terminal.span)?;
317-
self.output.push_str(&snip);
318+
self.output.push_str(&terminal.span.get_source_text(self.cx)?);
318319
}
319320
},
320321
True | False | Not(_) => {
@@ -345,8 +346,12 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
345346
}
346347
},
347348
&Term(n) => {
348-
let snip = snippet_opt(self.cx, self.terminals[n as usize].span.source_callsite())?;
349-
self.output.push_str(&snip);
349+
self.output.push_str(
350+
&self.terminals[n as usize]
351+
.span
352+
.source_callsite()
353+
.get_source_text(self.cx)?,
354+
);
350355
},
351356
}
352357
Some(())
@@ -370,8 +375,8 @@ fn simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<String> {
370375
_ => None,
371376
}
372377
.and_then(|op| {
373-
let lhs_snippet = snippet_opt(cx, lhs.span)?;
374-
let rhs_snippet = snippet_opt(cx, rhs.span)?;
378+
let lhs_snippet = lhs.span.get_source_text(cx)?;
379+
let rhs_snippet = rhs.span.get_source_text(cx)?;
375380

376381
if !(lhs_snippet.starts_with('(') && lhs_snippet.ends_with(')')) {
377382
if let (ExprKind::Cast(..), BinOpKind::Ge) = (&lhs.kind, binop.node) {
@@ -399,7 +404,7 @@ fn simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<String> {
399404
let path: &str = path.ident.name.as_str();
400405
a == path
401406
})
402-
.and_then(|(_, neg_method)| Some(format!("{}.{neg_method}()", snippet_opt(cx, receiver.span)?)))
407+
.and_then(|(_, neg_method)| Some(format!("{}.{neg_method}()", receiver.span.get_source_text(cx)?)))
403408
},
404409
_ => None,
405410
}

clippy_lints/src/borrow_deref_ref.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::reference::DEREF_ADDROF;
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::source::SpanRangeExt;
44
use clippy_utils::ty::implements_trait;
55
use clippy_utils::{get_parent_expr, is_from_proc_macro, is_lint_allowed};
66
use rustc_errors::Applicability;
@@ -73,6 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
7373
}
7474
})
7575
&& !is_from_proc_macro(cx, e)
76+
&& let Some(deref_text) = deref_target.span.get_source_text(cx)
7677
{
7778
span_lint_and_then(
7879
cx,
@@ -83,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
8384
diag.span_suggestion(
8485
e.span,
8586
"if you would like to reborrow, try removing `&*`",
86-
snippet_opt(cx, deref_target.span).unwrap(),
87+
deref_text.as_str(),
8788
Applicability::MachineApplicable,
8889
);
8990

@@ -98,7 +99,7 @@ impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
9899
diag.span_suggestion(
99100
e.span,
100101
"if you would like to deref, try using `&**`",
101-
format!("&**{}", &snippet_opt(cx, deref_target.span).unwrap()),
102+
format!("&**{deref_text}"),
102103
Applicability::MaybeIncorrect,
103104
);
104105
},

clippy_lints/src/format.rs

+2-2
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::macros::{find_format_arg_expr, root_macro_call_first_node, FormatArgsStorage};
3-
use clippy_utils::source::{snippet_opt, snippet_with_context};
3+
use clippy_utils::source::{snippet_with_context, SpanRangeExt};
44
use clippy_utils::sugg::Sugg;
55
use rustc_ast::{FormatArgsPiece, FormatOptions, FormatTrait};
66
use rustc_errors::Applicability;
@@ -65,7 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
6565
([], []) => span_useless_format_empty(cx, call_site, "String::new()".to_owned(), applicability),
6666
([], [_]) => {
6767
// Simulate macro expansion, converting {{ and }} to { and }.
68-
let Some(snippet) = snippet_opt(cx, format_args.span) else {
68+
let Some(snippet) = format_args.span.get_source_text(cx) else {
6969
return;
7070
};
7171
let s_expand = snippet.replace("{{", "{").replace("}}", "}");

clippy_lints/src/format_args.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use clippy_utils::macros::{
77
find_format_arg_expr, format_arg_removal_span, format_placeholder_format_span, is_assert_macro, is_format_macro,
88
is_panic, matching_root_macro_call, root_macro_call_first_node, FormatArgsStorage, FormatParamUsage, MacroCall,
99
};
10-
use clippy_utils::source::snippet_opt;
10+
use clippy_utils::source::SpanRangeExt;
1111
use clippy_utils::ty::{implements_trait, is_type_lang_item};
1212
use itertools::Itertools;
1313
use rustc_ast::{
@@ -407,7 +407,7 @@ impl<'a, 'tcx> FormatArgsExpr<'a, 'tcx> {
407407
count_needed_derefs(receiver_ty, cx.typeck_results().expr_adjustments(receiver).iter())
408408
&& implements_trait(cx, target, display_trait_id, &[])
409409
&& let Some(sized_trait_id) = cx.tcx.lang_items().sized_trait()
410-
&& let Some(receiver_snippet) = snippet_opt(cx, receiver.span.source_callsite())
410+
&& let Some(receiver_snippet) = receiver.span.source_callsite().get_source_text(cx)
411411
{
412412
let needs_ref = !implements_trait(cx, receiver_ty, sized_trait_id, &[]);
413413
if n_needed_derefs == 0 && !needs_ref {

clippy_lints/src/from_over_into.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_config::Conf;
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::macros::span_is_local;
55
use clippy_utils::path_def_id;
6-
use clippy_utils::source::snippet_opt;
6+
use clippy_utils::source::SpanRangeExt;
77
use rustc_errors::Applicability;
88
use rustc_hir::intravisit::{walk_path, Visitor};
99
use rustc_hir::{
@@ -178,8 +178,8 @@ fn convert_to_from(
178178
return None;
179179
};
180180

181-
let from = snippet_opt(cx, self_ty.span)?;
182-
let into = snippet_opt(cx, target_ty.span)?;
181+
let from = self_ty.span.get_source_text(cx)?;
182+
let into = target_ty.span.get_source_text(cx)?;
183183

184184
let return_type = matches!(sig.decl.output, FnRetTy::Return(_))
185185
.then_some(String::from("Self"))
@@ -190,10 +190,10 @@ fn convert_to_from(
190190
(into_trait_seg.ident.span, String::from("From")),
191191
// impl Into<T> for U -> impl Into<U> for U
192192
// ~ ~
193-
(target_ty.span, from.clone()),
193+
(target_ty.span, from.to_owned()),
194194
// impl Into<T> for U -> impl Into<T> for T
195195
// ~ ~
196-
(self_ty.span, into),
196+
(self_ty.span, into.to_owned()),
197197
// fn into(self) -> T -> fn from(self) -> T
198198
// ~~~~ ~~~~
199199
(impl_item.ident.span, String::from("from")),
@@ -223,7 +223,7 @@ fn convert_to_from(
223223
}
224224

225225
for span in finder.upper {
226-
suggestions.push((span, from.clone()));
226+
suggestions.push((span, from.to_owned()));
227227
}
228228
for span in finder.lower {
229229
suggestions.push((span, String::from("val")));

clippy_lints/src/int_plus_one.rs

+3-3
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::ast::{BinOpKind, Expr, ExprKind, LitKind};
44
use rustc_ast::token;
55
use rustc_errors::Applicability;
@@ -130,8 +130,8 @@ impl IntPlusOne {
130130
BinOpKind::Le => "<",
131131
_ => return None,
132132
};
133-
if let Some(snippet) = snippet_opt(cx, node.span) {
134-
if let Some(other_side_snippet) = snippet_opt(cx, other_side.span) {
133+
if let Some(snippet) = node.span.get_source_text(cx) {
134+
if let Some(other_side_snippet) = other_side.span.get_source_text(cx) {
135135
let rec = match side {
136136
Side::Lhs => Some(format!("{snippet} {binop_string} {other_side_snippet}")),
137137
Side::Rhs => Some(format!("{other_side_snippet} {binop_string} {snippet}")),

clippy_lints/src/items_after_test_module.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
2-
use clippy_utils::source::snippet_opt;
2+
use clippy_utils::source::SpanRangeExt;
33
use clippy_utils::{fulfill_or_allowed, is_cfg_test, is_from_proc_macro};
44
use rustc_errors::{Applicability, SuggestionStyle};
55
use rustc_hir::{HirId, Item, ItemKind, Mod};
@@ -93,11 +93,14 @@ impl LateLintPass<'_> for ItemsAfterTestModule {
9393
if let Some(prev) = mod_pos.checked_sub(1)
9494
&& let prev = cx.tcx.hir().item(module.item_ids[prev])
9595
&& let items_span = last.span.with_lo(test_mod.span.hi())
96-
&& let Some(items) = snippet_opt(cx, items_span)
96+
&& let Some(items) = items_span.get_source_text(cx)
9797
{
9898
diag.multipart_suggestion_with_style(
9999
"move the items to before the test module was defined",
100-
vec![(prev.span.shrink_to_hi(), items), (items_span, String::new())],
100+
vec![
101+
(prev.span.shrink_to_hi(), items.to_owned()),
102+
(items_span, String::new()),
103+
],
101104
Applicability::MachineApplicable,
102105
SuggestionStyle::HideCodeAlways,
103106
);

clippy_lints/src/large_stack_frames.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{fmt, ops};
33
use clippy_config::Conf;
44
use clippy_utils::diagnostics::span_lint_and_then;
55
use clippy_utils::fn_has_unsatisfiable_preds;
6-
use clippy_utils::source::snippet_opt;
6+
use clippy_utils::source::SpanRangeExt;
77
use rustc_hir::def_id::LocalDefId;
88
use rustc_hir::intravisit::FnKind;
99
use rustc_hir::{Body, FnDecl};
@@ -186,7 +186,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeStackFrames {
186186
// TODO: Is there a cleaner, robust way to ask this question?
187187
// The obvious `LocalDecl::is_user_variable()` panics on "unwrapping cross-crate data",
188188
// and that doesn't get us the true name in scope rather than the span text either.
189-
if let Some(name) = snippet_opt(cx, local_span)
189+
if let Some(name) = local_span.get_source_text(cx)
190190
&& is_ident(&name)
191191
{
192192
// If the local is an ordinary named variable,

clippy_lints/src/len_zero.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_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, Sugg};
44
use clippy_utils::{get_item_name, get_parent_as_impl, is_lint_allowed, peel_ref_operators};
55
use rustc_ast::ast::LitKind;
@@ -216,7 +216,7 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
216216
}
217217

218218
fn span_without_enclosing_paren(cx: &LateContext<'_>, span: Span) -> Span {
219-
let Some(snippet) = snippet_opt(cx, span) else {
219+
let Some(snippet) = span.get_source_text(cx) else {
220220
return span;
221221
};
222222
if has_enclosing_paren(snippet) {

clippy_lints/src/literal_representation.rs

+3-3
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::numeric_literal::{NumericLiteral, Radix};
4-
use clippy_utils::source::snippet_opt;
4+
use clippy_utils::source::SpanRangeExt;
55
use rustc_ast::ast::{Expr, ExprKind, LitKind};
66
use rustc_ast::token;
77
use rustc_errors::Applicability;
@@ -225,7 +225,7 @@ impl LiteralDigitGrouping {
225225
}
226226

227227
fn check_lit(&self, cx: &EarlyContext<'_>, lit: token::Lit, span: Span) {
228-
if let Some(src) = snippet_opt(cx, span)
228+
if let Some(src) = span.get_source_text(cx)
229229
&& let Ok(lit_kind) = LitKind::from_token_lit(lit)
230230
&& let Some(mut num_lit) = NumericLiteral::from_lit_kind(&src, &lit_kind)
231231
{
@@ -439,7 +439,7 @@ impl DecimalLiteralRepresentation {
439439
// Lint integral literals.
440440
if let Ok(lit_kind) = LitKind::from_token_lit(lit)
441441
&& let LitKind::Int(val, _) = lit_kind
442-
&& let Some(src) = snippet_opt(cx, span)
442+
&& let Some(src) = span.get_source_text(cx)
443443
&& let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit_kind)
444444
&& num_lit.radix == Radix::Decimal
445445
&& val >= u128::from(self.threshold)

clippy_lints/src/manual_float_methods.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::consts::{ConstEvalCtxt, Constant};
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::source::SpanRangeExt;
44
use clippy_utils::{is_from_proc_macro, path_to_local};
55
use rustc_errors::Applicability;
66
use rustc_hir::{BinOpKind, Constness, Expr, ExprKind};
@@ -103,7 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
103103
// case somebody does that for some reason
104104
&& (is_infinity(&const_1) && is_neg_infinity(&const_2)
105105
|| is_neg_infinity(&const_1) && is_infinity(&const_2))
106-
&& let Some(local_snippet) = snippet_opt(cx, first.span)
106+
&& let Some(local_snippet) = first.span.get_source_text(cx)
107107
{
108108
let variant = match (kind.node, lhs_kind.node, rhs_kind.node) {
109109
(BinOpKind::Or, BinOpKind::Eq, BinOpKind::Eq) => Variant::ManualIsInfinite,

clippy_lints/src/manual_hash_one.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_config::Conf;
33
use clippy_utils::diagnostics::span_lint_hir_and_then;
4-
use clippy_utils::source::snippet_opt;
4+
use clippy_utils::source::SpanRangeExt;
55
use clippy_utils::visitors::{is_local_used, local_used_once};
66
use clippy_utils::{is_trait_method, path_to_local_id};
77
use rustc_errors::Applicability;
@@ -107,8 +107,8 @@ impl LateLintPass<'_> for ManualHashOne {
107107
finish_expr.span,
108108
"manual implementation of `BuildHasher::hash_one`",
109109
|diag| {
110-
if let Some(build_hasher) = snippet_opt(cx, build_hasher.span)
111-
&& let Some(hashed_value) = snippet_opt(cx, hashed_value.span)
110+
if let Some(build_hasher) = build_hasher.span.get_source_text(cx)
111+
&& let Some(hashed_value) = hashed_value.span.get_source_text(cx)
112112
{
113113
diag.multipart_suggestion(
114114
"try",

clippy_lints/src/manual_non_exhaustive.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_config::msrvs::{self, Msrv};
22
use clippy_config::Conf;
33
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
44
use clippy_utils::is_doc_hidden;
5-
use clippy_utils::source::snippet_opt;
5+
use clippy_utils::source::SpanRangeExt;
66
use rustc_ast::ast::{self, VisibilityKind};
77
use rustc_ast::attr;
88
use rustc_data_structures::fx::FxHashSet;
@@ -124,7 +124,7 @@ impl EarlyLintPass for ManualNonExhaustiveStruct {
124124
|diag| {
125125
if !item.attrs.iter().any(|attr| attr.has_name(sym::non_exhaustive))
126126
&& let header_span = cx.sess().source_map().span_until_char(item.span, delimiter)
127-
&& let Some(snippet) = snippet_opt(cx, header_span)
127+
&& let Some(snippet) = header_span.get_source_text(cx)
128128
{
129129
diag.span_suggestion(
130130
header_span,
@@ -194,7 +194,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualNonExhaustiveEnum {
194194
"this seems like a manual implementation of the non-exhaustive pattern",
195195
|diag| {
196196
let header_span = cx.sess().source_map().span_until_char(enum_span, '{');
197-
if let Some(snippet) = snippet_opt(cx, header_span) {
197+
if let Some(snippet) = header_span.get_source_text(cx) {
198198
diag.span_suggestion(
199199
header_span,
200200
"add the attribute",

clippy_lints/src/manual_range_patterns.rs

+3-3
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::snippet_opt;
2+
use clippy_utils::source::SpanRangeExt;
33
use rustc_ast::LitKind;
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_errors::Applicability;
@@ -143,8 +143,8 @@ impl LateLintPass<'_> for ManualRangePatterns {
143143
pat.span,
144144
"this OR pattern can be rewritten using a range",
145145
|diag| {
146-
if let Some(min) = snippet_opt(cx, min.span)
147-
&& let Some(max) = snippet_opt(cx, max.span)
146+
if let Some(min) = min.span.get_source_text(cx)
147+
&& let Some(max) = max.span.get_source_text(cx)
148148
{
149149
diag.span_suggestion(
150150
pat.span,

0 commit comments

Comments
 (0)