Skip to content

Commit 83f2cd5

Browse files
committed
clean-up
1 parent f110f34 commit 83f2cd5

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

clippy_lints/src/needless_parens_on_range_literals.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use clippy_utils::diagnostics::span_lint_and_then;
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::higher;
3-
use clippy_utils::source::{snippet, snippet_with_applicability};
3+
use clippy_utils::source::{SpanRangeExt, snippet_with_applicability};
44

55
use rustc_ast::ast;
66
use rustc_errors::Applicability;
@@ -40,10 +40,6 @@ declare_clippy_lint! {
4040

4141
declare_lint_pass!(NeedlessParensOnRangeLiterals => [NEEDLESS_PARENS_ON_RANGE_LITERALS]);
4242

43-
fn snippet_enclosed_in_parenthesis(snippet: &str) -> bool {
44-
snippet.starts_with('(') && snippet.ends_with(')')
45-
}
46-
4743
fn check_for_parens(cx: &LateContext<'_>, e: &Expr<'_>, is_start: bool) {
4844
if is_start
4945
&& let ExprKind::Lit(literal) = e.kind
@@ -54,20 +50,20 @@ fn check_for_parens(cx: &LateContext<'_>, e: &Expr<'_>, is_start: bool) {
5450
}
5551
if let ExprKind::Lit(literal) = e.kind
5652
// the indicator that parenthesis surround the literal is that the span of the expression and the literal differ
57-
&& (literal.span.data().hi - literal.span.data().lo) != (e.span.data().hi - e.span.data().lo)
53+
&& literal.span != e.span
5854
// inspect the source code of the expression for parenthesis
59-
&& snippet_enclosed_in_parenthesis(&snippet(cx, e.span, ""))
55+
&& e.span.check_source_text(cx, |s| s.starts_with('(') && s.ends_with(')'))
6056
{
6157
let mut applicability = Applicability::MachineApplicable;
62-
span_lint_and_then(
58+
let suggestion = snippet_with_applicability(cx, literal.span, "_", &mut applicability);
59+
span_lint_and_sugg(
6360
cx,
6461
NEEDLESS_PARENS_ON_RANGE_LITERALS,
6562
e.span,
6663
"needless parenthesis on range literals can be removed",
67-
|diag| {
68-
let suggestion = snippet_with_applicability(cx, literal.span, "_", &mut applicability);
69-
diag.span_suggestion(e.span, "try", suggestion, applicability);
70-
},
64+
"try",
65+
suggestion.to_string(),
66+
applicability,
7167
);
7268
}
7369
}

tests/ui/needless_parens_on_range_literals.fixed

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
//@edition:2018
2-
31
#![warn(clippy::needless_parens_on_range_literals)]
4-
#![allow(clippy::almost_complete_range)]
2+
#![expect(clippy::almost_complete_range)]
53

64
fn main() {
75
let _ = 'a'..='z';

tests/ui/needless_parens_on_range_literals.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
//@edition:2018
2-
31
#![warn(clippy::needless_parens_on_range_literals)]
4-
#![allow(clippy::almost_complete_range)]
2+
#![expect(clippy::almost_complete_range)]
53

64
fn main() {
75
let _ = ('a')..=('z');

tests/ui/needless_parens_on_range_literals.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: needless parenthesis on range literals can be removed
2-
--> tests/ui/needless_parens_on_range_literals.rs:7:13
2+
--> tests/ui/needless_parens_on_range_literals.rs:5:13
33
|
44
LL | let _ = ('a')..=('z');
55
| ^^^^^ help: try: `'a'`
@@ -8,31 +8,31 @@ LL | let _ = ('a')..=('z');
88
= help: to override `-D warnings` add `#[allow(clippy::needless_parens_on_range_literals)]`
99

1010
error: needless parenthesis on range literals can be removed
11-
--> tests/ui/needless_parens_on_range_literals.rs:7:21
11+
--> tests/ui/needless_parens_on_range_literals.rs:5:21
1212
|
1313
LL | let _ = ('a')..=('z');
1414
| ^^^^^ help: try: `'z'`
1515

1616
error: needless parenthesis on range literals can be removed
17-
--> tests/ui/needless_parens_on_range_literals.rs:10:18
17+
--> tests/ui/needless_parens_on_range_literals.rs:8:18
1818
|
1919
LL | let _ = 'a'..('z');
2020
| ^^^^^ help: try: `'z'`
2121

2222
error: needless parenthesis on range literals can be removed
23-
--> tests/ui/needless_parens_on_range_literals.rs:13:19
23+
--> tests/ui/needless_parens_on_range_literals.rs:11:19
2424
|
2525
LL | let _ = (1.)..(2.);
2626
| ^^^^ help: try: `2.`
2727

2828
error: needless parenthesis on range literals can be removed
29-
--> tests/ui/needless_parens_on_range_literals.rs:15:13
29+
--> tests/ui/needless_parens_on_range_literals.rs:13:13
3030
|
3131
LL | let _ = ('a')..;
3232
| ^^^^^ help: try: `'a'`
3333

3434
error: needless parenthesis on range literals can be removed
35-
--> tests/ui/needless_parens_on_range_literals.rs:17:15
35+
--> tests/ui/needless_parens_on_range_literals.rs:15:15
3636
|
3737
LL | let _ = ..('z');
3838
| ^^^^^ help: try: `'z'`

0 commit comments

Comments
 (0)