Skip to content

Commit 64ff2b0

Browse files
Replace span_to_snippet calls with snippet_opt from clippy_utils
1 parent 2dbf0c1 commit 64ff2b0

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

clippy_lints/src/methods/from_iter_instead_of_collect.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet_opt;
23
use clippy_utils::ty::implements_trait;
34
use clippy_utils::{is_expr_path_def_path, paths, sugg};
45
use if_chain::if_chain;
56
use rustc_errors::Applicability;
67
use rustc_hir as hir;
7-
use rustc_lint::{LateContext, LintContext};
8+
use rustc_lint::LateContext;
89
use rustc_middle::ty::Ty;
910
use rustc_span::sym;
1011

@@ -43,7 +44,7 @@ fn extract_turbofish(cx: &LateContext<'_>, expr: &hir::Expr<'_>, ty: Ty<'tcx>) -
4344

4445
let call_site = expr.span.source_callsite();
4546
if_chain! {
46-
if let Ok(snippet) = cx.sess().source_map().span_to_snippet(call_site);
47+
if let Some(snippet) = snippet_opt(cx, call_site);
4748
let snippet_split = snippet.split("::").collect::<Vec<_>>();
4849
if let Some((_, elements)) = snippet_split.split_last();
4950

clippy_lints/src/misc_early/unneeded_field_pattern.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
2+
use clippy_utils::source::snippet_opt;
23
use rustc_ast::ast::{Pat, PatKind};
3-
use rustc_lint::{EarlyContext, LintContext};
4+
use rustc_lint::EarlyContext;
45

56
use super::UNNEEDED_FIELD_PATTERN;
67

@@ -48,7 +49,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
4849
match field.pat.kind {
4950
PatKind::Wild => {},
5051
_ => {
51-
if let Ok(n) = cx.sess().source_map().span_to_snippet(field.span) {
52+
if let Some(n) = snippet_opt(cx, field.span) {
5253
normal.push(n);
5354
}
5455
},

clippy_lints/src/reference.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ impl EarlyLintPass for DerefAddrOf {
5454
then {
5555
let mut applicability = Applicability::MachineApplicable;
5656
let sugg = if e.span.from_expansion() {
57-
if let Ok(macro_source) = cx.sess.source_map().span_to_snippet(e.span) {
57+
#[allow(clippy::option_if_let_else)]
58+
if let Some(macro_source) = snippet_opt(cx, e.span) {
5859
// Remove leading whitespace from the given span
5960
// e.g: ` $visitor` turns into `$visitor`
6061
let trim_leading_whitespaces = |span| {

clippy_lints/src/unused_unit.rs

Lines changed: 12 additions & 13 deletions
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::position_before_rarrow;
2+
use clippy_utils::source::{position_before_rarrow, snippet_opt};
33
use if_chain::if_chain;
44
use rustc_ast::ast;
55
use rustc_ast::visit::FnKind;
66
use rustc_errors::Applicability;
7-
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
7+
use rustc_lint::{EarlyContext, EarlyLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99
use rustc_span::source_map::Span;
1010
use rustc_span::BytePos;
@@ -125,17 +125,16 @@ fn is_unit_expr(expr: &ast::Expr) -> bool {
125125
}
126126

127127
fn lint_unneeded_unit_return(cx: &EarlyContext<'_>, ty: &ast::Ty, span: Span) {
128-
let (ret_span, appl) = if let Ok(fn_source) = cx.sess().source_map().span_to_snippet(span.with_hi(ty.span.hi())) {
129-
position_before_rarrow(&fn_source).map_or((ty.span, Applicability::MaybeIncorrect), |rpos| {
130-
(
131-
#[allow(clippy::cast_possible_truncation)]
132-
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
133-
Applicability::MachineApplicable,
134-
)
135-
})
136-
} else {
137-
(ty.span, Applicability::MaybeIncorrect)
138-
};
128+
let (ret_span, appl) =
129+
snippet_opt(cx, span.with_hi(ty.span.hi())).map_or((ty.span, Applicability::MaybeIncorrect), |fn_source| {
130+
position_before_rarrow(&fn_source).map_or((ty.span, Applicability::MaybeIncorrect), |rpos| {
131+
(
132+
#[allow(clippy::cast_possible_truncation)]
133+
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
134+
Applicability::MachineApplicable,
135+
)
136+
})
137+
});
139138
span_lint_and_sugg(
140139
cx,
141140
UNUSED_UNIT,

0 commit comments

Comments
 (0)