Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clippy_lints/src/attrs/non_minimal_cfg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{Attribute, NON_MINIMAL_CFG};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::SpanRangeExt;
use clippy_utils::source::SpanExt;
use rustc_ast::{MetaItemInner, MetaItemKind};
use rustc_errors::Applicability;
use rustc_lint::EarlyContext;
Expand Down Expand Up @@ -29,7 +29,7 @@ fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[MetaItemInner]) {
meta.span,
"unneeded sub `cfg` when there is only one condition",
|diag| {
if let Some(snippet) = list[0].span().get_source_text(cx) {
if let Some(snippet) = list[0].span().get_text(cx) {
diag.span_suggestion(
meta.span,
"try",
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/attrs/unnecessary_clippy_cfg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{Attribute, UNNECESSARY_CLIPPY_CFG};
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg};
use clippy_utils::source::SpanRangeExt;
use clippy_utils::source::SpanExt;
use itertools::Itertools;
use rustc_ast::AttrStyle;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -32,7 +32,7 @@ pub(super) fn check(
return;
}
if nb_items == clippy_lints.len() {
if let Some(snippet) = behind_cfg_attr.span.get_source_text(cx) {
if let Some(snippet) = behind_cfg_attr.span.get_text(cx) {
span_lint_and_sugg(
cx,
UNNECESSARY_CLIPPY_CFG,
Expand All @@ -48,7 +48,7 @@ pub(super) fn check(
);
}
} else {
let snippet = clippy_lints.iter().filter_map(|sp| sp.get_source_text(cx)).join(",");
let snippet = clippy_lints.iter().filter_map(|sp| sp.get_text(cx)).join(",");
span_lint_and_note(
cx,
UNNECESSARY_CLIPPY_CFG,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/attrs/useless_attribute.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::USELESS_ATTRIBUTE;
use super::utils::{is_lint_level, is_word, namespace_and_lint};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::{SpanRangeExt, first_line_of_span};
use clippy_utils::source::{SpanExt, first_line_of_span};
use clippy_utils::sym;
use rustc_ast::{Attribute, Item, ItemKind};
use rustc_errors::Applicability;
Expand Down Expand Up @@ -73,7 +73,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, item: &Item, attrs: &[Attribute]) {
}
let line_span = first_line_of_span(cx, attr.span);

if let Some(src) = line_span.get_source_text(cx)
if let Some(src) = line_span.get_text(cx)
&& src.contains("#[")
{
#[expect(clippy::collapsible_span_lint_calls)]
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/bool_to_int_with_if.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::HasSession;
use clippy_utils::sugg::Sugg;
use clippy_utils::{higher, is_else_clause, is_in_const_context, span_contains_comment};
use rustc_ast::LitKind;
Expand Down Expand Up @@ -59,7 +58,7 @@ impl<'tcx> LateLintPass<'tcx> for BoolToIntWithIf {
&& !is_in_const_context(cx)
{
let ty = cx.typeck_results().expr_ty(then);
let mut applicability = if span_contains_comment(cx.sess().source_map(), expr.span) {
let mut applicability = if span_contains_comment(cx, expr.span) {
Applicability::MaybeIncorrect
} else {
Applicability::MachineApplicable
Expand Down
31 changes: 12 additions & 19 deletions clippy_lints/src/booleans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
use clippy_utils::higher::has_let_expr;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::res::MaybeDef;
use clippy_utils::source::{SpanRangeExt, snippet_with_context};
use clippy_utils::source::{SpanExt, snippet_with_context};
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::implements_trait;
use clippy_utils::{eq_expr_value, sym};
Expand Down Expand Up @@ -153,30 +153,30 @@ fn check_inverted_bool_in_condition(

let suggestion = match (left.kind, right.kind) {
(ExprKind::Unary(UnOp::Not, left_sub), ExprKind::Unary(UnOp::Not, right_sub)) => {
let Some(left) = left_sub.span.get_source_text(cx) else {
let Some(left) = left_sub.span.get_text(cx) else {
return;
};
let Some(right) = right_sub.span.get_source_text(cx) else {
let Some(right) = right_sub.span.get_text(cx) else {
return;
};
let Some(op) = bin_op_eq_str(op) else { return };
format!("{left} {op} {right}")
},
(ExprKind::Unary(UnOp::Not, left_sub), _) => {
let Some(left) = left_sub.span.get_source_text(cx) else {
let Some(left) = left_sub.span.get_text(cx) else {
return;
};
let Some(right) = right.span.get_source_text(cx) else {
let Some(right) = right.span.get_text(cx) else {
return;
};
let Some(op) = inverted_bin_op_eq_str(op) else { return };
format!("{left} {op} {right}")
},
(_, ExprKind::Unary(UnOp::Not, right_sub)) => {
let Some(left) = left.span.get_source_text(cx) else {
let Some(left) = left.span.get_text(cx) else {
return;
};
let Some(right) = right_sub.span.get_source_text(cx) else {
let Some(right) = right_sub.span.get_text(cx) else {
return;
};
let Some(op) = inverted_bin_op_eq_str(op) else { return };
Expand Down Expand Up @@ -387,12 +387,8 @@ impl SuggestContext<'_, '_, '_> {
}
},
&Term(n) => {
self.output.push_str(
&self.terminals[n as usize]
.span
.source_callsite()
.get_source_text(self.cx)?,
);
self.output
.push_str(&self.terminals[n as usize].span.source_callsite().get_text(self.cx)?);
},
}
Some(())
Expand Down Expand Up @@ -447,24 +443,21 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: Msrv, expr: &Expr<'_>) -> Optio
.map(|arg| simplify_not(cx, curr_msrv, arg))
.collect::<Option<Vec<_>>>()?
.join(", ");
Some(format!(
"{}.{neg_method}({negated_args})",
receiver.span.get_source_text(cx)?
))
Some(format!("{}.{neg_method}({negated_args})", receiver.span.get_text(cx)?))
})
},
ExprKind::Closure(closure) => {
let body = cx.tcx.hir_body(closure.body);
let params = body
.params
.iter()
.map(|param| param.span.get_source_text(cx).map(|t| t.to_string()))
.map(|param| param.span.get_text(cx).map(|t| t.to_string()))
.collect::<Option<Vec<_>>>()?
.join(", ");
let negated = simplify_not(cx, curr_msrv, body.value)?;
Some(format!("|{params}| {negated}"))
},
ExprKind::Unary(UnOp::Not, expr) => expr.span.get_source_text(cx).map(|t| t.to_string()),
ExprKind::Unary(UnOp::Not, expr) => expr.span.get_text(cx).map(|t| t.to_string()),
_ => None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/borrow_deref_ref.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::reference::DEREF_ADDROF;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::SpanRangeExt;
use clippy_utils::source::SpanExt;
use clippy_utils::ty::implements_trait;
use clippy_utils::{get_parent_expr, is_expr_temporary_value, is_from_proc_macro, is_lint_allowed, is_mutable};
use rustc_errors::Applicability;
Expand Down Expand Up @@ -79,7 +79,7 @@ impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
// If the new borrow might be itself borrowed mutably and the original reference is not a temporary
// value, do not propose to use it directly.
&& (is_expr_temporary_value(cx, deref_target) || !potentially_bound_to_mutable_ref(cx, e))
&& let Some(deref_text) = deref_target.span.get_source_text(cx)
&& let Some(deref_text) = deref_target.span.get_text(cx)
{
span_lint_and_then(
cx,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/casts/as_ptr_cast_mut.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::SpanRangeExt;
use clippy_utils::source::SpanExt;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::LateContext;
Expand All @@ -19,7 +19,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
&& let as_ptr_sig = cx.tcx.fn_sig(as_ptr_did).instantiate_identity()
&& let Some(first_param_ty) = as_ptr_sig.skip_binder().inputs().iter().next()
&& let ty::Ref(_, _, Mutability::Not) = first_param_ty.kind()
&& let Some(recv) = receiver.span.get_source_text(cx)
&& let Some(recv) = receiver.span.get_text(cx)
{
// `as_mut_ptr` might not exist
let applicability = Applicability::MaybeIncorrect;
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/casts/cast_lossless.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_in_const_context;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::SpanRangeExt;
use clippy_utils::source::SpanExt;
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::is_isize_or_usize;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -40,7 +40,7 @@ pub(super) fn check(
diag.help("an `as` cast can become silently lossy if the types change in the future");
let mut applicability = Applicability::MachineApplicable;
let from_sugg = Sugg::hir_with_context(cx, cast_from_expr, expr.span.ctxt(), "<from>", &mut applicability);
let Some(ty) = hygiene::walk_chain(cast_to_hir.span, expr.span.ctxt()).get_source_text(cx) else {
let Some(ty) = hygiene::walk_chain(cast_to_hir.span, expr.span.ctxt()).get_text(cx) else {
return;
};
match cast_to_hir.kind {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/casts/manual_dangling_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::res::{MaybeDef, MaybeResPath};
use clippy_utils::source::SpanRangeExt;
use clippy_utils::source::SpanExt;
use clippy_utils::{expr_or_init, std_or_core, sym};
use rustc_ast::LitKind;
use rustc_errors::Applicability;
Expand All @@ -23,7 +23,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to:

let sugg = if let TyKind::Infer(()) = ptr_ty.ty.kind {
format!("{std_or_core}::{sugg_fn}()")
} else if let Some(mut_ty_snip) = ptr_ty.ty.span.get_source_text(cx) {
} else if let Some(mut_ty_snip) = ptr_ty.ty.span.get_text(cx) {
format!("{std_or_core}::{sugg_fn}::<{mut_ty_snip}>()")
} else {
return;
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/casts/unnecessary_cast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::numeric_literal::NumericLiteral;
use clippy_utils::res::MaybeResPath;
use clippy_utils::source::{SpanRangeExt, snippet_opt};
use clippy_utils::source::{SpanExt, snippet_opt};
use clippy_utils::visitors::{Visitable, for_each_expr_without_closures};
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias};
use rustc_ast::{LitFloatType, LitIntType, LitKind};
Expand Down Expand Up @@ -106,7 +106,7 @@ pub(super) fn check<'tcx>(
let literal_str = &cast_str;

if let LitKind::Int(n, _) = lit.node
&& let Some(src) = cast_expr.span.get_source_text(cx)
&& let Some(src) = cast_expr.span.get_text(cx)
&& cast_to.is_floating_point()
&& let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node)
&& let from_nbits = 128 - n.get().leading_zeros()
Expand All @@ -133,7 +133,7 @@ pub(super) fn check<'tcx>(
| LitKind::Float(_, LitFloatType::Suffixed(_))
if cast_from.kind() == cast_to.kind() =>
{
if let Some(src) = cast_expr.span.get_source_text(cx)
if let Some(src) = cast_expr.span.get_text(cx)
&& let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node)
{
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
Expand Down Expand Up @@ -281,7 +281,7 @@ fn is_cast_from_ty_alias<'tcx>(cx: &LateContext<'tcx>, expr: impl Visitable<'tcx
let res = cx.qpath_res(&qpath, expr.hir_id);
// Function call
if let Res::Def(DefKind::Fn, def_id) = res {
let Some(snippet) = cx.tcx.def_span(def_id).get_source_text(cx) else {
let Some(snippet) = cx.tcx.def_span(def_id).get_text(cx) else {
return ControlFlow::Continue(());
};
// This is the worst part of this entire function. This is the only way I know of to
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/casts/zero_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::SpanRangeExt;
use clippy_utils::source::SpanExt;
use clippy_utils::{is_in_const_context, is_integer_literal, std_or_core};
use rustc_errors::Applicability;
use rustc_hir::{Expr, Mutability, Ty, TyKind};
Expand All @@ -21,7 +21,7 @@ pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>

let sugg = if let TyKind::Infer(()) = mut_ty.ty.kind {
format!("{std_or_core}::{sugg_fn}()")
} else if let Some(mut_ty_snip) = mut_ty.ty.span.get_source_text(cx) {
} else if let Some(mut_ty_snip) = mut_ty.ty.span.get_text(cx) {
format!("{std_or_core}::{sugg_fn}::<{mut_ty_snip}>()")
} else {
return;
Expand Down
12 changes: 6 additions & 6 deletions clippy_lints/src/cognitive_complexity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::res::MaybeDef;
use clippy_utils::source::{IntoSpan, SpanRangeExt};
use clippy_utils::source::{FileRangeExt, SpanExt, StrExt};
use clippy_utils::visitors::for_each_expr_without_closures;
use clippy_utils::{LimitStack, get_async_fn_body, sym};
use core::ops::ControlFlow;
Expand Down Expand Up @@ -109,12 +109,12 @@ impl CognitiveComplexity {
let fn_span = match kind {
FnKind::ItemFn(ident, _, _) | FnKind::Method(ident, _) => ident.span,
FnKind::Closure => {
let header_span = body_span.with_hi(decl.output.span().lo());
if let Some(range) = header_span.map_range(cx, |_, src, range| {
let mut idxs = src.get(range.clone())?.match_indices('|');
Some(range.start + idxs.next()?.0..range.start + idxs.next()?.0 + 1)
if let Some(sp) = body_span.map_range(cx, |scx, range| {
range
.shrink_end_to(scx, decl.output.span().lo_ctxt())?
.map_range_text(scx, |src| src.find_bounded_inclusive('|'))
}) {
range.with_ctxt(header_span.ctxt())
sp
} else {
return;
}
Expand Down
30 changes: 14 additions & 16 deletions clippy_lints/src/collapsible_if.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::msrvs::Msrv;
use clippy_utils::source::{IntoSpan as _, SpanRangeExt, snippet, snippet_block_with_applicability};
use clippy_utils::source::{FileRangeExt, SpanExt, StrExt, snippet, snippet_block_with_applicability};
use clippy_utils::{can_use_if_let_chains, span_contains_non_whitespace, sym, tokenize_with_text};
use rustc_ast::{BinOpKind, MetaItemInner};
use rustc_errors::Applicability;
Expand Down Expand Up @@ -113,15 +113,15 @@ impl CollapsibleIf {
span_extract_keyword(cx.tcx.sess.source_map(), up_to_else, "else")
&& let Some(else_if_keyword_span) =
span_extract_keyword(cx.tcx.sess.source_map(), else_before_if, "if")
&& let Some(else_keyword_span) =
else_keyword_span.map_range(cx, |scx, range| range.with_leading_whitespace(scx))
&& let Some([else_open_bracket, else_closing_bracket]) =
else_block.span.map_range(cx, |scx, range| {
range
.map_range_text(scx, |src| src.get_prefix_suffix('{', '}'))?
.try_map(|r| r.with_leading_whitespace(scx))
})
{
let else_keyword_span = else_keyword_span.with_leading_whitespace(cx).into_span();
let else_open_bracket = else_block.span.split_at(1).0.with_leading_whitespace(cx).into_span();
let else_closing_bracket = {
let end = else_block.span.shrink_to_hi();
end.with_lo(end.lo() - BytePos(1))
.with_leading_whitespace(cx)
.into_span()
};
let sugg = vec![
// Remove the outer else block `else`
(else_keyword_span, String::new()),
Expand Down Expand Up @@ -170,6 +170,11 @@ impl CollapsibleIf {
&& self.eligible_condition(cx, check_inner)
&& expr.span.eq_ctxt(inner.span)
&& self.check_significant_tokens_and_expect_attrs(cx, then, inner, sym::collapsible_if)
&& let Some([then_open_bracket, then_closing_bracket]) = then.span.map_range(cx, |scx, range| {
range
.map_range_text(scx, |src| src.get_prefix_suffix('{', '}'))?
.try_map(|r| r.with_leading_whitespace(scx))
})
{
span_lint_hir_and_then(
cx,
Expand All @@ -178,13 +183,6 @@ impl CollapsibleIf {
expr.span,
"this `if` statement can be collapsed",
|diag| {
let then_open_bracket = then.span.split_at(1).0.with_leading_whitespace(cx).into_span();
let then_closing_bracket = {
let end = then.span.shrink_to_hi();
end.with_lo(end.lo() - BytePos(1))
.with_leading_whitespace(cx)
.into_span()
};
let (paren_start, inner_if_span, paren_end) = peel_parens(cx.tcx.sess.source_map(), inner.span);
let inner_if = inner_if_span.split_at(2).0;
let mut sugg = vec![
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/default_constructed_unit_structs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_ty_alias;
use clippy_utils::source::SpanRangeExt as _;
use clippy_utils::source::SpanExt as _;
use hir::ExprKind;
use hir::def::Res;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -75,7 +75,7 @@ impl LateLintPass<'_> for DefaultConstructedUnitStructs {
&& !base.is_suggestable_infer_ty()
{
let mut removals = vec![(expr.span.with_lo(qpath.qself_span().hi()), String::new())];
if expr.span.check_source_text(cx, |s| s.starts_with('<')) {
if expr.span.check_text(cx, |s| s.starts_with('<')) {
// Remove `<`, '>` has already been removed by the existing removal expression.
removals.push((expr.span.with_hi(qpath.qself_span().lo()), String::new()));
}
Expand Down
Loading