Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace span_lint with span_lint_and_sugg along with error message #6895

Merged
merged 2 commits into from
Mar 14, 2021
Merged
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
16 changes: 14 additions & 2 deletions clippy_lints/src/casts/unnecessary_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_middle::ty::{self, FloatTy, InferTy, Ty};

use if_chain::if_chain;

use crate::utils::{numeric_literal::NumericLiteral, snippet_opt, span_lint, span_lint_and_sugg};
use crate::utils::{numeric_literal::NumericLiteral, snippet_opt, span_lint_and_sugg};

use super::UNNECESSARY_CAST;

Expand Down Expand Up @@ -44,16 +44,28 @@ pub(super) fn check(
lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
},
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
LitKind::Int(_, LitIntType::Signed(_) | LitIntType::Unsigned(_))
| LitKind::Float(_, LitFloatType::Suffixed(_))
if cast_from.kind() == cast_to.kind() =>
{
if let Some(src) = snippet_opt(cx, lit.span) {
let num_lit = NumericLiteral::from_lit_kind(&src, &lit.node).unwrap();
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
}
},
_ => {
if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
span_lint(
span_lint_and_sugg(
cx,
UNNECESSARY_CAST,
expr.span,
&format!(
"casting to the same type is unnecessary (`{}` -> `{}`)",
cast_from, cast_to
),
"try",
literal_str,
Applicability::MachineApplicable,
);
return true;
}
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/unnecessary_cast.stderr
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
error: casting to the same type is unnecessary (`i32` -> `i32`)
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:6:5
|
LL | 1i32 as i32;
| ^^^^^^^^^^^
| ^^^^^^^^^^^ help: try: `1_i32`
|
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`

error: casting to the same type is unnecessary (`f32` -> `f32`)
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:7:5
|
LL | 1f32 as f32;
| ^^^^^^^^^^^
| ^^^^^^^^^^^ help: try: `1_f32`

error: casting to the same type is unnecessary (`bool` -> `bool`)
--> $DIR/unnecessary_cast.rs:8:5
|
LL | false as bool;
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ help: try: `false`

error: aborting due to 3 previous errors