Skip to content

Commit 6e84f00

Browse files
committed
Check MSRV for bool to int from impl
1 parent 96db1d6 commit 6e84f00

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

clippy_lints/src/casts/cast_lossless.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::in_constant;
32
use clippy_utils::source::snippet_opt;
43
use clippy_utils::ty::is_isize_or_usize;
4+
use clippy_utils::{in_constant, meets_msrv, msrvs};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind};
77
use rustc_lint::LateContext;
88
use rustc_middle::ty::{self, FloatTy, Ty};
9+
use rustc_semver::RustcVersion;
910

1011
use super::{utils, CAST_LOSSLESS};
1112

12-
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_op: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) {
13-
if !should_lint(cx, expr, cast_from, cast_to) {
13+
pub(super) fn check(
14+
cx: &LateContext<'_>,
15+
expr: &Expr<'_>,
16+
cast_op: &Expr<'_>,
17+
cast_from: Ty<'_>,
18+
cast_to: Ty<'_>,
19+
msrv: &Option<RustcVersion>,
20+
) {
21+
if !should_lint(cx, expr, cast_from, cast_to, msrv) {
1422
return;
1523
}
1624

@@ -46,7 +54,13 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_op: &Expr<'_>, c
4654
);
4755
}
4856

49-
fn should_lint(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>) -> bool {
57+
fn should_lint(
58+
cx: &LateContext<'_>,
59+
expr: &Expr<'_>,
60+
cast_from: Ty<'_>,
61+
cast_to: Ty<'_>,
62+
msrv: &Option<RustcVersion>,
63+
) -> bool {
5064
// Do not suggest using From in consts/statics until it is valid to do so (see #2267).
5165
if in_constant(cx, expr.hir_id) {
5266
return false;
@@ -72,7 +86,7 @@ fn should_lint(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to
7286
};
7387
from_nbits < to_nbits
7488
},
75-
(false, true) if matches!(cast_from.kind(), ty::Bool) => true,
89+
(false, true) if matches!(cast_from.kind(), ty::Bool) && meets_msrv(msrv.as_ref(), &msrvs::FROM_BOOL) => true,
7690
(_, _) => {
7791
matches!(cast_from.kind(), ty::Float(FloatTy::F32)) && matches!(cast_to.kind(), ty::Float(FloatTy::F64))
7892
},

clippy_lints/src/casts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
435435
cast_sign_loss::check(cx, expr, cast_expr, cast_from, cast_to);
436436
}
437437

438-
cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to);
438+
cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, &self.msrv);
439439
}
440440
}
441441

clippy_utils/src/msrvs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ msrv_aliases! {
2828
1,35,0 { OPTION_COPIED, RANGE_CONTAINS }
2929
1,34,0 { TRY_FROM }
3030
1,30,0 { ITERATOR_FIND_MAP, TOOL_ATTRIBUTES }
31+
1,28,0 { FROM_BOOL }
3132
1,17,0 { FIELD_INIT_SHORTHAND, STATIC_IN_CONST }
3233
1,16,0 { STR_REPEAT }
3334
}

tests/ui/min_rust_version_attr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ fn unnest_or_patterns() {
140140
#[cfg_attr(rustfmt, rustfmt_skip)]
141141
fn deprecated_cfg_attr() {}
142142

143+
#[warn(clippy::cast_lossless)]
144+
fn int_from_bool() -> u8 {
145+
true as u8
146+
}
147+
143148
fn main() {
144149
filter_map_next();
145150
checked_conversion();
@@ -156,6 +161,7 @@ fn main() {
156161
map_unwrap_or();
157162
missing_const_for_fn();
158163
unnest_or_patterns();
164+
int_from_bool();
159165
}
160166

161167
mod just_under_msrv {

tests/ui/min_rust_version_attr.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
error: stripping a prefix manually
2-
--> $DIR/min_rust_version_attr.rs:180:24
2+
--> $DIR/min_rust_version_attr.rs:186:24
33
|
44
LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
55
| ^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::manual-strip` implied by `-D warnings`
88
note: the prefix was tested here
9-
--> $DIR/min_rust_version_attr.rs:179:9
9+
--> $DIR/min_rust_version_attr.rs:185:9
1010
|
1111
LL | if s.starts_with("hello, ") {
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -17,13 +17,13 @@ LL ~ assert_eq!(<stripped>.to_uppercase(), "WORLD!");
1717
|
1818

1919
error: stripping a prefix manually
20-
--> $DIR/min_rust_version_attr.rs:192:24
20+
--> $DIR/min_rust_version_attr.rs:198:24
2121
|
2222
LL | assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
2323
| ^^^^^^^^^^^^^^^^^^^^
2424
|
2525
note: the prefix was tested here
26-
--> $DIR/min_rust_version_attr.rs:191:9
26+
--> $DIR/min_rust_version_attr.rs:197:9
2727
|
2828
LL | if s.starts_with("hello, ") {
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)