Skip to content

Commit 497177f

Browse files
committed
Auto merge of rust-lang#13296 - Jarcho:get_src_display3, r=Alexendoo
Replace more uses of `snippet_opt`. Almost all calls are removed after this. changelog: none
2 parents 301f2c4 + 9450b4a commit 497177f

20 files changed

+125
-109
lines changed

clippy_lints/src/casts/cast_lossless.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::is_in_const_context;
4-
use clippy_utils::source::snippet_opt;
4+
use clippy_utils::source::SpanRangeExt;
55
use clippy_utils::sugg::Sugg;
66
use clippy_utils::ty::is_isize_or_usize;
77
use rustc_errors::Applicability;
@@ -34,7 +34,7 @@ pub(super) fn check(
3434
diag.help("an `as` cast can become silently lossy if the types change in the future");
3535
let mut applicability = Applicability::MachineApplicable;
3636
let from_sugg = Sugg::hir_with_context(cx, cast_from_expr, expr.span.ctxt(), "<from>", &mut applicability);
37-
let Some(ty) = snippet_opt(cx, hygiene::walk_chain(cast_to_hir.span, expr.span.ctxt())) else {
37+
let Some(ty) = hygiene::walk_chain(cast_to_hir.span, expr.span.ctxt()).get_source_text(cx) else {
3838
return;
3939
};
4040
match cast_to_hir.kind {

clippy_lints/src/casts/unnecessary_cast.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::numeric_literal::NumericLiteral;
3-
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::source::{snippet_opt, SpanRangeExt};
44
use clippy_utils::visitors::{for_each_expr_without_closures, Visitable};
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};
@@ -104,7 +104,7 @@ pub(super) fn check<'tcx>(
104104
let literal_str = &cast_str;
105105

106106
if let LitKind::Int(n, _) = lit.node
107-
&& let Some(src) = snippet_opt(cx, cast_expr.span)
107+
&& let Some(src) = cast_expr.span.get_source_text(cx)
108108
&& cast_to.is_floating_point()
109109
&& let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node)
110110
&& let from_nbits = 128 - n.get().leading_zeros()
@@ -131,7 +131,7 @@ pub(super) fn check<'tcx>(
131131
| LitKind::Float(_, LitFloatType::Suffixed(_))
132132
if cast_from.kind() == cast_to.kind() =>
133133
{
134-
if let Some(src) = snippet_opt(cx, cast_expr.span) {
134+
if let Some(src) = cast_expr.span.get_source_text(cx) {
135135
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) {
136136
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
137137
return true;
@@ -253,7 +253,7 @@ fn is_cast_from_ty_alias<'tcx>(cx: &LateContext<'tcx>, expr: impl Visitable<'tcx
253253
let res = cx.qpath_res(&qpath, expr.hir_id);
254254
// Function call
255255
if let Res::Def(DefKind::Fn, def_id) = res {
256-
let Some(snippet) = snippet_opt(cx, cx.tcx.def_span(def_id)) else {
256+
let Some(snippet) = cx.tcx.def_span(def_id).get_source_text(cx) else {
257257
return ControlFlow::Continue(());
258258
};
259259
// This is the worst part of this entire function. This is the only way I know of to

clippy_lints/src/casts/zero_ptr.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 clippy_utils::{is_in_const_context, is_integer_literal, std_or_core};
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, Mutability, Ty, TyKind};
@@ -20,7 +20,7 @@ pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>
2020

2121
let sugg = if let TyKind::Infer = mut_ty.ty.kind {
2222
format!("{std_or_core}::{sugg_fn}()")
23-
} else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) {
23+
} else if let Some(mut_ty_snip) = mut_ty.ty.span.get_source_text(cx) {
2424
format!("{std_or_core}::{sugg_fn}::<{mut_ty_snip}>()")
2525
} else {
2626
return;

clippy_lints/src/functions/must_use.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_span::{sym, Span};
1212

1313
use clippy_utils::attrs::is_proc_macro;
1414
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
15-
use clippy_utils::source::snippet_opt;
15+
use clippy_utils::source::SpanRangeExt;
1616
use clippy_utils::ty::is_must_use_ty;
1717
use clippy_utils::visitors::for_each_expr_without_closures;
1818
use clippy_utils::{return_ty, trait_ref_of_method};
@@ -155,7 +155,7 @@ fn check_must_use_candidate<'tcx>(
155155
return;
156156
}
157157
span_lint_and_then(cx, MUST_USE_CANDIDATE, fn_span, msg, |diag| {
158-
if let Some(snippet) = snippet_opt(cx, fn_span) {
158+
if let Some(snippet) = fn_span.get_source_text(cx) {
159159
diag.span_suggestion(
160160
fn_span,
161161
"add the attribute",

clippy_lints/src/functions/too_many_lines.rs

+45-46
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
use clippy_utils::diagnostics::span_lint;
2+
use clippy_utils::source::SpanRangeExt;
13
use rustc_hir as hir;
24
use rustc_hir::intravisit::FnKind;
35
use rustc_lint::{LateContext, LintContext};
46
use rustc_middle::lint::in_external_macro;
57
use rustc_span::Span;
68

7-
use clippy_utils::diagnostics::span_lint;
8-
use clippy_utils::source::snippet_opt;
9-
109
use super::TOO_MANY_LINES;
1110

1211
pub(super) fn check_fn(
@@ -22,57 +21,57 @@ pub(super) fn check_fn(
2221
return;
2322
}
2423

25-
let Some(code_snippet) = snippet_opt(cx, body.value.span) else {
26-
return;
27-
};
2824
let mut line_count: u64 = 0;
29-
let mut in_comment = false;
30-
let mut code_in_line;
25+
let too_many = body.value.span.check_source_text(cx, |src| {
26+
let mut in_comment = false;
27+
let mut code_in_line;
3128

32-
let function_lines = if matches!(body.value.kind, hir::ExprKind::Block(..))
33-
&& code_snippet.as_bytes().first().copied() == Some(b'{')
34-
&& code_snippet.as_bytes().last().copied() == Some(b'}')
35-
{
36-
// Removing the braces from the enclosing block
37-
&code_snippet[1..code_snippet.len() - 1]
38-
} else {
39-
&code_snippet
40-
}
41-
.trim() // Remove leading and trailing blank lines
42-
.lines();
29+
let function_lines = if matches!(body.value.kind, hir::ExprKind::Block(..))
30+
&& src.as_bytes().first().copied() == Some(b'{')
31+
&& src.as_bytes().last().copied() == Some(b'}')
32+
{
33+
// Removing the braces from the enclosing block
34+
&src[1..src.len() - 1]
35+
} else {
36+
src
37+
}
38+
.trim() // Remove leading and trailing blank lines
39+
.lines();
4340

44-
for mut line in function_lines {
45-
code_in_line = false;
46-
loop {
47-
line = line.trim_start();
48-
if line.is_empty() {
49-
break;
50-
}
51-
if in_comment {
52-
if let Some(i) = line.find("*/") {
53-
line = &line[i + 2..];
54-
in_comment = false;
55-
continue;
41+
for mut line in function_lines {
42+
code_in_line = false;
43+
loop {
44+
line = line.trim_start();
45+
if line.is_empty() {
46+
break;
5647
}
57-
} else {
58-
let multi_idx = line.find("/*").unwrap_or(line.len());
59-
let single_idx = line.find("//").unwrap_or(line.len());
60-
code_in_line |= multi_idx > 0 && single_idx > 0;
61-
// Implies multi_idx is below line.len()
62-
if multi_idx < single_idx {
63-
line = &line[multi_idx + 2..];
64-
in_comment = true;
65-
continue;
48+
if in_comment {
49+
if let Some(i) = line.find("*/") {
50+
line = &line[i + 2..];
51+
in_comment = false;
52+
continue;
53+
}
54+
} else {
55+
let multi_idx = line.find("/*").unwrap_or(line.len());
56+
let single_idx = line.find("//").unwrap_or(line.len());
57+
code_in_line |= multi_idx > 0 && single_idx > 0;
58+
// Implies multi_idx is below line.len()
59+
if multi_idx < single_idx {
60+
line = &line[multi_idx + 2..];
61+
in_comment = true;
62+
continue;
63+
}
6664
}
65+
break;
66+
}
67+
if code_in_line {
68+
line_count += 1;
6769
}
68-
break;
69-
}
70-
if code_in_line {
71-
line_count += 1;
7270
}
73-
}
71+
line_count > too_many_lines_threshold
72+
});
7473

75-
if line_count > too_many_lines_threshold {
74+
if too_many {
7675
span_lint(
7776
cx,
7877
TOO_MANY_LINES,

clippy_lints/src/matches/manual_unwrap_or.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::consts::ConstEvalCtxt;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::source::{indent_of, reindent_multiline, snippet_opt};
3+
use clippy_utils::source::{indent_of, reindent_multiline, SpanRangeExt};
44
use clippy_utils::ty::is_type_diagnostic_item;
55
use clippy_utils::usage::contains_return_break_continue_macro;
66
use clippy_utils::{is_res_lang_ctor, path_to_local_id, peel_blocks, sugg};
@@ -67,11 +67,11 @@ fn check_and_lint<'tcx>(
6767
&& path_to_local_id(peel_blocks(then_expr), binding_hir_id)
6868
&& cx.typeck_results().expr_adjustments(then_expr).is_empty()
6969
&& let Some(ty_name) = find_type_name(cx, ty)
70-
&& let Some(or_body_snippet) = snippet_opt(cx, else_expr.span)
70+
&& let Some(or_body_snippet) = else_expr.span.get_source_text(cx)
7171
&& let Some(indent) = indent_of(cx, expr.span)
7272
&& ConstEvalCtxt::new(cx).eval_simple(else_expr).is_some()
7373
{
74-
lint(cx, expr, let_expr, ty_name, or_body_snippet, indent);
74+
lint(cx, expr, let_expr, ty_name, &or_body_snippet, indent);
7575
}
7676
}
7777

@@ -110,7 +110,7 @@ fn lint<'tcx>(
110110
expr: &Expr<'tcx>,
111111
scrutinee: &'tcx Expr<'_>,
112112
ty_name: &str,
113-
or_body_snippet: String,
113+
or_body_snippet: &str,
114114
indent: usize,
115115
) {
116116
let reindented_or_body = reindent_multiline(or_body_snippet.into(), true, Some(indent));

clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs

+7-5
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::{indent_of, reindent_multiline, snippet_opt};
2+
use clippy_utils::source::{indent_of, reindent_multiline, SpanRangeExt};
33
use clippy_utils::ty::is_type_lang_item;
44
use rustc_ast::ast::LitKind;
55
use rustc_errors::Applicability;
@@ -49,10 +49,12 @@ pub(super) fn check<'tcx>(
4949
"case-sensitive file extension comparison",
5050
|diag| {
5151
diag.help("consider using a case-insensitive comparison instead");
52-
if let Some(mut recv_source) = snippet_opt(cx, recv.span) {
53-
if !cx.typeck_results().expr_ty(recv).is_ref() {
54-
recv_source = format!("&{recv_source}");
55-
}
52+
if let Some(recv_source) = recv.span.get_source_text(cx) {
53+
let recv_source = if cx.typeck_results().expr_ty(recv).is_ref() {
54+
recv_source.to_owned()
55+
} else {
56+
format!("&{recv_source}")
57+
};
5658

5759
let suggestion_source = reindent_multiline(
5860
format!(

clippy_lints/src/methods/filter_map_bool_then.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::FILTER_MAP_BOOL_THEN;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::paths::BOOL_THEN;
4-
use clippy_utils::source::snippet_opt;
4+
use clippy_utils::source::SpanRangeExt;
55
use clippy_utils::ty::is_copy;
66
use clippy_utils::{is_from_proc_macro, is_trait_method, match_def_path, peel_blocks};
77
use rustc_errors::Applicability;
@@ -42,9 +42,9 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
4242
.iter()
4343
.filter(|adj| matches!(adj.kind, Adjust::Deref(_)))
4444
.count()
45-
&& let Some(param_snippet) = snippet_opt(cx, param.span)
46-
&& let Some(filter) = snippet_opt(cx, recv.span)
47-
&& let Some(map) = snippet_opt(cx, then_body.span)
45+
&& let Some(param_snippet) = param.span.get_source_text(cx)
46+
&& let Some(filter) = recv.span.get_source_text(cx)
47+
&& let Some(map) = then_body.span.get_source_text(cx)
4848
{
4949
span_lint_and_sugg(
5050
cx,

clippy_lints/src/methods/from_iter_instead_of_collect.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 clippy_utils::ty::implements_trait;
44
use clippy_utils::{is_path_diagnostic_item, sugg};
55
use rustc_errors::Applicability;
@@ -39,7 +39,7 @@ fn extract_turbofish(cx: &LateContext<'_>, expr: &hir::Expr<'_>, ty: Ty<'_>) ->
3939
}
4040

4141
let call_site = expr.span.source_callsite();
42-
if let Some(snippet) = snippet_opt(cx, call_site)
42+
if let Some(snippet) = call_site.get_source_text(cx)
4343
&& let snippet_split = snippet.split("::").collect::<Vec<_>>()
4444
&& let Some((_, elements)) = snippet_split.split_last()
4545
{

clippy_lints/src/methods/join_absolute_paths.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::expr_or_init;
3-
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::source::snippet;
44
use clippy_utils::ty::is_type_diagnostic_item;
55
use rustc_ast::ast::LitKind;
66
use rustc_errors::Applicability;
@@ -25,7 +25,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, recv: &'tcx Expr<'tcx>, join_a
2525
join_arg.span,
2626
"argument to `Path::join` starts with a path separator",
2727
|diag| {
28-
let arg_str = snippet_opt(cx, spanned.span).unwrap_or_else(|| "..".to_string());
28+
let arg_str = snippet(cx, spanned.span, "..");
2929

3030
let no_separator = if sym_str.starts_with('/') {
3131
arg_str.replacen('/', "", 1)

clippy_lints/src/methods/manual_ok_or.rs

+4-4
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::{indent_of, reindent_multiline, snippet_opt};
2+
use clippy_utils::source::{indent_of, reindent_multiline, SpanRangeExt};
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use clippy_utils::{is_res_lang_ctor, path_res, path_to_local_id};
55
use rustc_errors::Applicability;
@@ -23,11 +23,11 @@ pub(super) fn check<'tcx>(
2323
&& let ExprKind::Call(err_path, [err_arg]) = or_expr.kind
2424
&& is_res_lang_ctor(cx, path_res(cx, err_path), ResultErr)
2525
&& is_ok_wrapping(cx, map_expr)
26-
&& let Some(recv_snippet) = snippet_opt(cx, recv.span)
27-
&& let Some(err_arg_snippet) = snippet_opt(cx, err_arg.span)
26+
&& let Some(recv_snippet) = recv.span.get_source_text(cx)
27+
&& let Some(err_arg_snippet) = err_arg.span.get_source_text(cx)
2828
&& let Some(indent) = indent_of(cx, expr.span)
2929
{
30-
let reindented_err_arg_snippet = reindent_multiline(err_arg_snippet.into(), true, Some(indent + 4));
30+
let reindented_err_arg_snippet = reindent_multiline(err_arg_snippet.as_str().into(), true, Some(indent + 4));
3131
span_lint_and_sugg(
3232
cx,
3333
MANUAL_OK_OR,

clippy_lints/src/methods/manual_try_fold.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::source::snippet_opt;
3+
use clippy_utils::source::SpanRangeExt;
44
use clippy_utils::ty::implements_trait;
55
use clippy_utils::{is_from_proc_macro, is_trait_method};
66
use rustc_errors::Applicability;
@@ -31,13 +31,15 @@ pub(super) fn check<'tcx>(
3131
&& let Res::Def(DefKind::Ctor(_, _), _) = cx.qpath_res(&qpath, path.hir_id)
3232
&& let ExprKind::Closure(closure) = acc.kind
3333
&& !is_from_proc_macro(cx, expr)
34-
&& let Some(args_snip) = closure.fn_arg_span.and_then(|fn_arg_span| snippet_opt(cx, fn_arg_span))
34+
&& let Some(args_snip) = closure
35+
.fn_arg_span
36+
.and_then(|fn_arg_span| fn_arg_span.get_source_text(cx))
3537
{
3638
let init_snip = rest
3739
.is_empty()
3840
.then_some(first.span)
39-
.and_then(|span| snippet_opt(cx, span))
40-
.unwrap_or("...".to_owned());
41+
.and_then(|span| span.get_source_text(cx))
42+
.map_or_else(|| "...".to_owned(), |src| src.to_owned());
4143

4244
span_lint_and_sugg(
4345
cx,

clippy_lints/src/methods/needless_character_iteration.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::Span;
77
use super::utils::get_last_chain_binding_hir_id;
88
use super::NEEDLESS_CHARACTER_ITERATION;
99
use clippy_utils::diagnostics::span_lint_and_sugg;
10-
use clippy_utils::source::snippet_opt;
10+
use clippy_utils::source::SpanRangeExt;
1111
use clippy_utils::{match_def_path, path_to_local_id, peel_blocks};
1212

1313
fn peels_expr_ref<'a, 'tcx>(mut expr: &'a Expr<'tcx>) -> &'a Expr<'tcx> {
@@ -35,7 +35,7 @@ fn handle_expr(
3535
&& path_to_local_id(receiver, first_param)
3636
&& let char_arg_ty = cx.typeck_results().expr_ty_adjusted(receiver).peel_refs()
3737
&& *char_arg_ty.kind() == ty::Char
38-
&& let Some(snippet) = snippet_opt(cx, before_chars)
38+
&& let Some(snippet) = before_chars.get_source_text(cx)
3939
{
4040
span_lint_and_sugg(
4141
cx,
@@ -79,7 +79,7 @@ fn handle_expr(
7979
&& let Some(fn_def_id) = cx.qpath_res(&path, fn_path.hir_id).opt_def_id()
8080
&& match_def_path(cx, fn_def_id, &["core", "char", "methods", "<impl char>", "is_ascii"])
8181
&& path_to_local_id(peels_expr_ref(arg), first_param)
82-
&& let Some(snippet) = snippet_opt(cx, before_chars)
82+
&& let Some(snippet) = before_chars.get_source_text(cx)
8383
{
8484
span_lint_and_sugg(
8585
cx,

0 commit comments

Comments
 (0)