Skip to content

Commit ac35e0e

Browse files
committed
Turn of unnecessary_cast for cfg-dependant types
1 parent 3e7c6de commit ac35e0e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

clippy_lints/src/types.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use if_chain::if_chain;
88
use rustc_ast::{FloatTy, IntTy, LitFloatType, LitIntType, LitKind, UintTy};
99
use rustc_errors::{Applicability, DiagnosticBuilder};
1010
use rustc_hir as hir;
11+
use rustc_hir::def::Res;
1112
use rustc_hir::intravisit::{walk_body, walk_expr, walk_ty, FnKind, NestedVisitorMap, Visitor};
1213
use rustc_hir::{
1314
BinOpKind, Block, Body, Expr, ExprKind, FnDecl, FnRetTy, FnSig, GenericArg, GenericBounds, GenericParamKind, HirId,
@@ -1632,7 +1633,14 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
16321633
if expr.span.from_expansion() {
16331634
return;
16341635
}
1635-
if let ExprKind::Cast(ref ex, _) = expr.kind {
1636+
if let ExprKind::Cast(ref ex, cast_to) = expr.kind {
1637+
if let TyKind::Path(QPath::Resolved(_, path)) = cast_to.kind {
1638+
if let Res::Def(_, def_id) = path.res {
1639+
if cx.tcx.has_attr(def_id, sym::cfg) || cx.tcx.has_attr(def_id, sym::cfg_attr) {
1640+
return;
1641+
}
1642+
}
1643+
}
16361644
let (cast_from, cast_to) = (cx.typeck_results().expr_ty(ex), cx.typeck_results().expr_ty(expr));
16371645
lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to);
16381646
if let Some(lit) = get_numeric_literal(ex) {

tests/ui/unnecessary_cast.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ fn main() {
2020
foo!(a, i32);
2121
foo!(b, f32);
2222
foo!(c, f64);
23+
24+
// do not lint cast to cfg-dependant type
25+
1 as std::os::raw::c_char;
2326
}

0 commit comments

Comments
 (0)