Skip to content

Commit b7892c6

Browse files
committed
Refactor to make getting position just before RArrow a common function
1 parent 1624b00 commit b7892c6

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

clippy_lints/src/manual_async_fn.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::paths::FUTURE_FROM_GENERATOR;
2-
use crate::utils::{match_function_call, snippet_block, snippet_opt, span_lint_and_then};
2+
use crate::utils::{match_function_call, position_before_rarrow, snippet_block, snippet_opt, span_lint_and_then};
33
use if_chain::if_chain;
44
use rustc_errors::Applicability;
55
use rustc_hir::intravisit::FnKind;
@@ -69,20 +69,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn {
6969
|diag| {
7070
if_chain! {
7171
if let Some(header_snip) = snippet_opt(cx, header_span);
72-
if let Some(ret_pos) = header_snip.rfind("->").map(|rpos| {
73-
let mut rpos = rpos;
74-
let chars: Vec<char> = header_snip.chars().collect();
75-
while rpos > 1 {
76-
if let Some(c) = chars.get(rpos - 1) {
77-
if c.is_whitespace() {
78-
rpos -= 1;
79-
continue;
80-
}
81-
}
82-
break;
83-
}
84-
rpos
85-
});
72+
if let Some(ret_pos) = position_before_rarrow(header_snip.clone());
8673
if let Some((ret_sugg, ret_snip)) = suggested_ret(cx, output);
8774
then {
8875
let help = format!("make the function `async` and {}", ret_sugg);

clippy_lints/src/unused_unit.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
77
use rustc_span::source_map::Span;
88
use rustc_span::BytePos;
99

10-
use crate::utils::span_lint_and_sugg;
10+
use crate::utils::{position_before_rarrow, span_lint_and_sugg};
1111

1212
declare_clippy_lint! {
1313
/// **What it does:** Checks for unit (`()`) expressions that can be removed.
@@ -120,26 +120,13 @@ fn is_unit_expr(expr: &ast::Expr) -> bool {
120120

121121
fn lint_unneeded_unit_return(cx: &EarlyContext<'_>, ty: &ast::Ty, span: Span) {
122122
let (ret_span, appl) = if let Ok(fn_source) = cx.sess().source_map().span_to_snippet(span.with_hi(ty.span.hi())) {
123-
fn_source
124-
.rfind("->")
125-
.map_or((ty.span, Applicability::MaybeIncorrect), |rpos| {
126-
let mut rpos = rpos;
127-
let chars: Vec<char> = fn_source.chars().collect();
128-
while rpos > 1 {
129-
if let Some(c) = chars.get(rpos - 1) {
130-
if c.is_whitespace() {
131-
rpos -= 1;
132-
continue;
133-
}
134-
}
135-
break;
136-
}
137-
(
138-
#[allow(clippy::cast_possible_truncation)]
139-
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
140-
Applicability::MachineApplicable,
141-
)
142-
})
123+
position_before_rarrow(fn_source).map_or((ty.span, Applicability::MaybeIncorrect), |rpos| {
124+
(
125+
#[allow(clippy::cast_possible_truncation)]
126+
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
127+
Applicability::MachineApplicable,
128+
)
129+
})
143130
} else {
144131
(ty.span, Applicability::MaybeIncorrect)
145132
};

clippy_lints/src/utils/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,35 @@ pub fn indent_of<T: LintContext>(cx: &T, span: Span) -> Option<usize> {
659659
snippet_opt(cx, line_span(cx, span)).and_then(|snip| snip.find(|c: char| !c.is_whitespace()))
660660
}
661661

662+
/// Returns the positon just before rarrow
663+
///
664+
/// ```rust,ignore
665+
/// fn into(self) -> () {}
666+
/// ^
667+
/// // in case of unformatted code
668+
/// fn into2(self)-> () {}
669+
/// ^
670+
/// fn into3(self) -> () {}
671+
/// ^
672+
/// ```
673+
#[allow(clippy::needless_pass_by_value)]
674+
pub fn position_before_rarrow(s: String) -> Option<usize> {
675+
s.rfind("->").map(|rpos| {
676+
let mut rpos = rpos;
677+
let chars: Vec<char> = s.chars().collect();
678+
while rpos > 1 {
679+
if let Some(c) = chars.get(rpos - 1) {
680+
if c.is_whitespace() {
681+
rpos -= 1;
682+
continue;
683+
}
684+
}
685+
break;
686+
}
687+
rpos
688+
})
689+
}
690+
662691
/// Extends the span to the beginning of the spans line, incl. whitespaces.
663692
///
664693
/// ```rust,ignore

0 commit comments

Comments
 (0)