Skip to content

Commit 0fce0bc

Browse files
committed
Rework clippy_utils::source.
Rename `get_source_text` and `check_source_text` to `get_text` and `check_text`.
1 parent 2fe1120 commit 0fce0bc

File tree

93 files changed

+1531
-591
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1531
-591
lines changed

clippy_lints/src/attrs/non_minimal_cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[MetaItemInner]) {
2929
meta.span,
3030
"unneeded sub `cfg` when there is only one condition",
3131
|diag| {
32-
if let Some(snippet) = list[0].span().get_source_text(cx) {
32+
if let Some(snippet) = list[0].span().get_text(cx) {
3333
diag.span_suggestion(
3434
meta.span,
3535
"try",

clippy_lints/src/attrs/unnecessary_clippy_cfg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(super) fn check(
3232
return;
3333
}
3434
if nb_items == clippy_lints.len() {
35-
if let Some(snippet) = behind_cfg_attr.span.get_source_text(cx) {
35+
if let Some(snippet) = behind_cfg_attr.span.get_text(cx) {
3636
span_lint_and_sugg(
3737
cx,
3838
UNNECESSARY_CLIPPY_CFG,
@@ -48,7 +48,7 @@ pub(super) fn check(
4848
);
4949
}
5050
} else {
51-
let snippet = clippy_lints.iter().filter_map(|sp| sp.get_source_text(cx)).join(",");
51+
let snippet = clippy_lints.iter().filter_map(|sp| sp.get_text(cx)).join(",");
5252
span_lint_and_note(
5353
cx,
5454
UNNECESSARY_CLIPPY_CFG,

clippy_lints/src/attrs/useless_attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, item: &Item, attrs: &[Attribute]) {
7373
}
7474
let line_span = first_line_of_span(cx, attr.span);
7575

76-
if let Some(src) = line_span.get_source_text(cx)
76+
if let Some(src) = line_span.get_text(cx)
7777
&& src.contains("#[")
7878
{
7979
#[expect(clippy::collapsible_span_lint_calls)]

clippy_lints/src/booleans.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -153,30 +153,30 @@ fn check_inverted_bool_in_condition(
153153

154154
let suggestion = match (left.kind, right.kind) {
155155
(ExprKind::Unary(UnOp::Not, left_sub), ExprKind::Unary(UnOp::Not, right_sub)) => {
156-
let Some(left) = left_sub.span.get_source_text(cx) else {
156+
let Some(left) = left_sub.span.get_text(cx) else {
157157
return;
158158
};
159-
let Some(right) = right_sub.span.get_source_text(cx) else {
159+
let Some(right) = right_sub.span.get_text(cx) else {
160160
return;
161161
};
162162
let Some(op) = bin_op_eq_str(op) else { return };
163163
format!("{left} {op} {right}")
164164
},
165165
(ExprKind::Unary(UnOp::Not, left_sub), _) => {
166-
let Some(left) = left_sub.span.get_source_text(cx) else {
166+
let Some(left) = left_sub.span.get_text(cx) else {
167167
return;
168168
};
169-
let Some(right) = right.span.get_source_text(cx) else {
169+
let Some(right) = right.span.get_text(cx) else {
170170
return;
171171
};
172172
let Some(op) = inverted_bin_op_eq_str(op) else { return };
173173
format!("{left} {op} {right}")
174174
},
175175
(_, ExprKind::Unary(UnOp::Not, right_sub)) => {
176-
let Some(left) = left.span.get_source_text(cx) else {
176+
let Some(left) = left.span.get_text(cx) else {
177177
return;
178178
};
179-
let Some(right) = right_sub.span.get_source_text(cx) else {
179+
let Some(right) = right_sub.span.get_text(cx) else {
180180
return;
181181
};
182182
let Some(op) = inverted_bin_op_eq_str(op) else { return };
@@ -387,12 +387,8 @@ impl SuggestContext<'_, '_, '_> {
387387
}
388388
},
389389
&Term(n) => {
390-
self.output.push_str(
391-
&self.terminals[n as usize]
392-
.span
393-
.source_callsite()
394-
.get_source_text(self.cx)?,
395-
);
390+
self.output
391+
.push_str(&self.terminals[n as usize].span.source_callsite().get_text(self.cx)?);
396392
},
397393
}
398394
Some(())
@@ -447,24 +443,21 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: Msrv, expr: &Expr<'_>) -> Optio
447443
.map(|arg| simplify_not(cx, curr_msrv, arg))
448444
.collect::<Option<Vec<_>>>()?
449445
.join(", ");
450-
Some(format!(
451-
"{}.{neg_method}({negated_args})",
452-
receiver.span.get_source_text(cx)?
453-
))
446+
Some(format!("{}.{neg_method}({negated_args})", receiver.span.get_text(cx)?))
454447
})
455448
},
456449
ExprKind::Closure(closure) => {
457450
let body = cx.tcx.hir_body(closure.body);
458451
let params = body
459452
.params
460453
.iter()
461-
.map(|param| param.span.get_source_text(cx).map(|t| t.to_string()))
454+
.map(|param| param.span.get_text(cx).map(|t| t.to_string()))
462455
.collect::<Option<Vec<_>>>()?
463456
.join(", ");
464457
let negated = simplify_not(cx, curr_msrv, body.value)?;
465458
Some(format!("|{params}| {negated}"))
466459
},
467-
ExprKind::Unary(UnOp::Not, expr) => expr.span.get_source_text(cx).map(|t| t.to_string()),
460+
ExprKind::Unary(UnOp::Not, expr) => expr.span.get_text(cx).map(|t| t.to_string()),
468461
_ => None,
469462
}
470463
}

clippy_lints/src/borrow_deref_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
7979
// If the new borrow might be itself borrowed mutably and the original reference is not a temporary
8080
// value, do not propose to use it directly.
8181
&& (is_expr_temporary_value(cx, deref_target) || !potentially_bound_to_mutable_ref(cx, e))
82-
&& let Some(deref_text) = deref_target.span.get_source_text(cx)
82+
&& let Some(deref_text) = deref_target.span.get_text(cx)
8383
{
8484
span_lint_and_then(
8585
cx,

clippy_lints/src/casts/as_ptr_cast_mut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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) = receiver.span.get_source_text(cx)
22+
&& let Some(recv) = receiver.span.get_text(cx)
2323
{
2424
// `as_mut_ptr` might not exist
2525
let applicability = Applicability::MaybeIncorrect;

clippy_lints/src/casts/cast_lossless.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub(super) fn check(
4040
diag.help("an `as` cast can become silently lossy if the types change in the future");
4141
let mut applicability = Applicability::MachineApplicable;
4242
let from_sugg = Sugg::hir_with_context(cx, cast_from_expr, expr.span.ctxt(), "<from>", &mut applicability);
43-
let Some(ty) = hygiene::walk_chain(cast_to_hir.span, expr.span.ctxt()).get_source_text(cx) else {
43+
let Some(ty) = hygiene::walk_chain(cast_to_hir.span, expr.span.ctxt()).get_text(cx) else {
4444
return;
4545
};
4646
match cast_to_hir.kind {

clippy_lints/src/casts/manual_dangling_ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to:
2323

2424
let sugg = if let TyKind::Infer(()) = ptr_ty.ty.kind {
2525
format!("{std_or_core}::{sugg_fn}()")
26-
} else if let Some(mut_ty_snip) = ptr_ty.ty.span.get_source_text(cx) {
26+
} else if let Some(mut_ty_snip) = ptr_ty.ty.span.get_text(cx) {
2727
format!("{std_or_core}::{sugg_fn}::<{mut_ty_snip}>()")
2828
} else {
2929
return;

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub(super) fn check<'tcx>(
106106
let literal_str = &cast_str;
107107

108108
if let LitKind::Int(n, _) = lit.node
109-
&& let Some(src) = cast_expr.span.get_source_text(cx)
109+
&& let Some(src) = cast_expr.span.get_text(cx)
110110
&& cast_to.is_floating_point()
111111
&& let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node)
112112
&& let from_nbits = 128 - n.get().leading_zeros()
@@ -133,7 +133,7 @@ pub(super) fn check<'tcx>(
133133
| LitKind::Float(_, LitFloatType::Suffixed(_))
134134
if cast_from.kind() == cast_to.kind() =>
135135
{
136-
if let Some(src) = cast_expr.span.get_source_text(cx)
136+
if let Some(src) = cast_expr.span.get_text(cx)
137137
&& let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node)
138138
{
139139
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
@@ -281,7 +281,7 @@ fn is_cast_from_ty_alias<'tcx>(cx: &LateContext<'tcx>, expr: impl Visitable<'tcx
281281
let res = cx.qpath_res(&qpath, expr.hir_id);
282282
// Function call
283283
if let Res::Def(DefKind::Fn, def_id) = res {
284-
let Some(snippet) = cx.tcx.def_span(def_id).get_source_text(cx) else {
284+
let Some(snippet) = cx.tcx.def_span(def_id).get_text(cx) else {
285285
return ControlFlow::Continue(());
286286
};
287287
// 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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>
2121

2222
let sugg = if let TyKind::Infer(()) = mut_ty.ty.kind {
2323
format!("{std_or_core}::{sugg_fn}()")
24-
} else if let Some(mut_ty_snip) = mut_ty.ty.span.get_source_text(cx) {
24+
} else if let Some(mut_ty_snip) = mut_ty.ty.span.get_text(cx) {
2525
format!("{std_or_core}::{sugg_fn}::<{mut_ty_snip}>()")
2626
} else {
2727
return;

0 commit comments

Comments
 (0)