mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 01:05:39 +03:00
fix: checked_conversions wrongly unmangled macros
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use clippy_config::Conf;
|
||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::msrvs::{self, Msrv};
|
||||
use clippy_utils::source::snippet_with_applicability;
|
||||
use clippy_utils::source::snippet_with_context;
|
||||
use clippy_utils::{SpanlessEq, is_in_const_context, is_integer_literal, sym};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{BinOpKind, Expr, ExprKind, QPath, TyKind};
|
||||
@@ -80,7 +80,8 @@ fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) {
|
||||
&& self.msrv.meets(cx, msrvs::TRY_FROM)
|
||||
{
|
||||
let mut applicability = Applicability::MachineApplicable;
|
||||
let snippet = snippet_with_applicability(cx, cv.expr_to_cast.span, "_", &mut applicability);
|
||||
let (snippet, _) =
|
||||
snippet_with_context(cx, cv.expr_to_cast.span, item.span.ctxt(), "_", &mut applicability);
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
CHECKED_CONVERSIONS,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#![allow(
|
||||
clippy::cast_lossless,
|
||||
clippy::legacy_numeric_constants,
|
||||
clippy::no_effect,
|
||||
unused,
|
||||
// Int::max_value will be deprecated in the future
|
||||
deprecated,
|
||||
@@ -105,4 +106,19 @@ fn msrv_1_34() {
|
||||
//~^ checked_conversions
|
||||
}
|
||||
|
||||
fn issue16293() {
|
||||
struct Outer {
|
||||
inner: u32,
|
||||
}
|
||||
let outer = Outer { inner: 42 };
|
||||
macro_rules! dot_inner {
|
||||
($obj:expr) => {
|
||||
$obj.inner
|
||||
};
|
||||
}
|
||||
|
||||
i32::try_from(dot_inner!(outer)).is_ok();
|
||||
//~^ checked_conversions
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#![allow(
|
||||
clippy::cast_lossless,
|
||||
clippy::legacy_numeric_constants,
|
||||
clippy::no_effect,
|
||||
unused,
|
||||
// Int::max_value will be deprecated in the future
|
||||
deprecated,
|
||||
@@ -105,4 +106,19 @@ fn msrv_1_34() {
|
||||
//~^ checked_conversions
|
||||
}
|
||||
|
||||
fn issue16293() {
|
||||
struct Outer {
|
||||
inner: u32,
|
||||
}
|
||||
let outer = Outer { inner: 42 };
|
||||
macro_rules! dot_inner {
|
||||
($obj:expr) => {
|
||||
$obj.inner
|
||||
};
|
||||
}
|
||||
|
||||
dot_inner!(outer) <= i32::MAX as u32;
|
||||
//~^ checked_conversions
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:15:13
|
||||
--> tests/ui/checked_conversions.rs:16:13
|
||||
|
|
||||
LL | let _ = value <= (u32::max_value() as i64) && value >= 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()`
|
||||
@@ -8,100 +8,106 @@ LL | let _ = value <= (u32::max_value() as i64) && value >= 0;
|
||||
= help: to override `-D warnings` add `#[allow(clippy::checked_conversions)]`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:17:13
|
||||
--> tests/ui/checked_conversions.rs:18:13
|
||||
|
|
||||
LL | let _ = value <= (u32::MAX as i64) && value >= 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:22:13
|
||||
--> tests/ui/checked_conversions.rs:23:13
|
||||
|
|
||||
LL | let _ = value <= i64::from(u16::max_value()) && value >= 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:24:13
|
||||
--> tests/ui/checked_conversions.rs:25:13
|
||||
|
|
||||
LL | let _ = value <= i64::from(u16::MAX) && value >= 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:29:13
|
||||
--> tests/ui/checked_conversions.rs:30:13
|
||||
|
|
||||
LL | let _ = value <= (u8::max_value() as isize) && value >= 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u8::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:31:13
|
||||
--> tests/ui/checked_conversions.rs:32:13
|
||||
|
|
||||
LL | let _ = value <= (u8::MAX as isize) && value >= 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u8::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:38:13
|
||||
--> tests/ui/checked_conversions.rs:39:13
|
||||
|
|
||||
LL | let _ = value <= (i32::max_value() as i64) && value >= (i32::min_value() as i64);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:40:13
|
||||
--> tests/ui/checked_conversions.rs:41:13
|
||||
|
|
||||
LL | let _ = value <= (i32::MAX as i64) && value >= (i32::MIN as i64);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:45:13
|
||||
--> tests/ui/checked_conversions.rs:46:13
|
||||
|
|
||||
LL | let _ = value <= i64::from(i16::max_value()) && value >= i64::from(i16::min_value());
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i16::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:47:13
|
||||
--> tests/ui/checked_conversions.rs:48:13
|
||||
|
|
||||
LL | let _ = value <= i64::from(i16::MAX) && value >= i64::from(i16::MIN);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i16::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:54:13
|
||||
--> tests/ui/checked_conversions.rs:55:13
|
||||
|
|
||||
LL | let _ = value <= i32::max_value() as u32;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:56:13
|
||||
--> tests/ui/checked_conversions.rs:57:13
|
||||
|
|
||||
LL | let _ = value <= i32::MAX as u32;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:61:13
|
||||
--> tests/ui/checked_conversions.rs:62:13
|
||||
|
|
||||
LL | let _ = value <= isize::max_value() as usize && value as i32 == 5;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `isize::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:63:13
|
||||
--> tests/ui/checked_conversions.rs:64:13
|
||||
|
|
||||
LL | let _ = value <= isize::MAX as usize && value as i32 == 5;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `isize::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:68:13
|
||||
--> tests/ui/checked_conversions.rs:69:13
|
||||
|
|
||||
LL | let _ = value <= u16::max_value() as u32 && value as i32 == 5;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:70:13
|
||||
--> tests/ui/checked_conversions.rs:71:13
|
||||
|
|
||||
LL | let _ = value <= u16::MAX as u32 && value as i32 == 5;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u16::try_from(value).is_ok()`
|
||||
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:104:13
|
||||
--> tests/ui/checked_conversions.rs:105:13
|
||||
|
|
||||
LL | let _ = value <= (u32::max_value() as i64) && value >= 0;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `u32::try_from(value).is_ok()`
|
||||
|
||||
error: aborting due to 17 previous errors
|
||||
error: checked cast can be simplified
|
||||
--> tests/ui/checked_conversions.rs:120:5
|
||||
|
|
||||
LL | dot_inner!(outer) <= i32::MAX as u32;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `i32::try_from(dot_inner!(outer)).is_ok()`
|
||||
|
||||
error: aborting due to 18 previous errors
|
||||
|
||||
|
||||
Reference in New Issue
Block a user