Skip to content

Commit b57bf6b

Browse files
Fix cast_possible_truncation should not suggest inside const context (#15164)
Closes #15163 This lint should not give suggestions in const context for now, since a lot of the commonly used method of `Result` is not const. changelog: [`cast_possible_truncation`] fix improperly give suggestions inside const context
2 parents 1f76a23 + 9cba70b commit b57bf6b

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

clippy_lints/src/casts/cast_possible_truncation.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
33
use clippy_utils::source::snippet;
44
use clippy_utils::sugg::Sugg;
55
use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize};
6-
use clippy_utils::{expr_or_init, sym};
6+
use clippy_utils::{expr_or_init, is_in_const_context, sym};
77
use rustc_abi::IntegerType;
88
use rustc_errors::{Applicability, Diag};
99
use rustc_hir::def::{DefKind, Res};
@@ -168,7 +168,9 @@ pub(super) fn check(
168168

169169
span_lint_and_then(cx, CAST_POSSIBLE_TRUNCATION, expr.span, msg, |diag| {
170170
diag.help("if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...");
171-
if !cast_from.is_floating_point() {
171+
// TODO: Remove the condition for const contexts when `try_from` and other commonly used methods
172+
// become const fn.
173+
if !is_in_const_context(cx) && !cast_from.is_floating_point() {
172174
offer_suggestion(cx, expr, cast_expr, cast_to_span, diag);
173175
}
174176
});

tests/ui/cast_size.32bit.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ error: casting `usize` to `f64` causes a loss of precision on targets with 64-bi
177177
LL | 9_999_999_999_999_999usize as f64;
178178
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
179179

180+
error: casting `usize` to `u16` may truncate the value
181+
--> tests/ui/cast_size.rs:71:20
182+
|
183+
LL | const N: u16 = M as u16;
184+
| ^^^^^^^^
185+
|
186+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
187+
180188
error: literal out of range for `usize`
181189
--> tests/ui/cast_size.rs:63:5
182190
|
@@ -186,5 +194,5 @@ LL | 9_999_999_999_999_999usize as f64;
186194
= note: the literal `9_999_999_999_999_999usize` does not fit into the type `usize` whose range is `0..=4294967295`
187195
= note: `#[deny(overflowing_literals)]` on by default
188196

189-
error: aborting due to 19 previous errors
197+
error: aborting due to 20 previous errors
190198

tests/ui/cast_size.64bit.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,13 @@ error: casting `usize` to `f64` causes a loss of precision on targets with 64-bi
177177
LL | 9_999_999_999_999_999usize as f64;
178178
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
179179

180-
error: aborting due to 18 previous errors
180+
error: casting `usize` to `u16` may truncate the value
181+
--> tests/ui/cast_size.rs:71:20
182+
|
183+
LL | const N: u16 = M as u16;
184+
| ^^^^^^^^
185+
|
186+
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
187+
188+
error: aborting due to 19 previous errors
181189

tests/ui/cast_size.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ fn main() {
6565
//~[32bit]^^ ERROR: literal out of range for `usize`
6666
// 999_999_999_999_999_999_999_999_999_999u128 as f128;
6767
}
68+
69+
fn issue15163() {
70+
const M: usize = 100;
71+
const N: u16 = M as u16;
72+
//~^ cast_possible_truncation
73+
}

0 commit comments

Comments
 (0)