diff options
| author | Cameron Steffen <cam.steffen94@gmail.com> | 2020-11-22 19:21:01 -0600 |
|---|---|---|
| committer | Cameron Steffen <cam.steffen94@gmail.com> | 2020-11-22 19:35:04 -0600 |
| commit | a39a93faeb4969fac62e896138cdf72377fa5502 (patch) | |
| tree | 14276ab60447a10b56b6aa8b5dba4f97a9aa649f | |
| parent | 3e7c6dec244539970b593824334876f8b6ed0b18 (diff) | |
| download | rust-a39a93faeb4969fac62e896138cdf72377fa5502.tar.gz rust-a39a93faeb4969fac62e896138cdf72377fa5502.zip | |
Disable unnecessary_cast for cfg-dependant types
| -rw-r--r-- | clippy_lints/src/types.rs | 10 | ||||
| -rw-r--r-- | tests/ui/unnecessary_cast.rs | 3 |
2 files changed, 12 insertions, 1 deletions
diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index f0e10e374e1..840adbbc57a 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -8,6 +8,7 @@ use if_chain::if_chain; use rustc_ast::{FloatTy, IntTy, LitFloatType, LitIntType, LitKind, UintTy}; use rustc_errors::{Applicability, DiagnosticBuilder}; use rustc_hir as hir; +use rustc_hir::def::Res; use rustc_hir::intravisit::{walk_body, walk_expr, walk_ty, FnKind, NestedVisitorMap, Visitor}; use rustc_hir::{ BinOpKind, Block, Body, Expr, ExprKind, FnDecl, FnRetTy, FnSig, GenericArg, GenericBounds, GenericParamKind, HirId, @@ -1632,7 +1633,14 @@ impl<'tcx> LateLintPass<'tcx> for Casts { if expr.span.from_expansion() { return; } - if let ExprKind::Cast(ref ex, _) = expr.kind { + if let ExprKind::Cast(ref ex, cast_to) = expr.kind { + if let TyKind::Path(QPath::Resolved(_, path)) = cast_to.kind { + if let Res::Def(_, def_id) = path.res { + if cx.tcx.has_attr(def_id, sym::cfg) || cx.tcx.has_attr(def_id, sym::cfg_attr) { + return; + } + } + } let (cast_from, cast_to) = (cx.typeck_results().expr_ty(ex), cx.typeck_results().expr_ty(expr)); lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to); if let Some(lit) = get_numeric_literal(ex) { diff --git a/tests/ui/unnecessary_cast.rs b/tests/ui/unnecessary_cast.rs index df9b227eeb3..e8f2fb46665 100644 --- a/tests/ui/unnecessary_cast.rs +++ b/tests/ui/unnecessary_cast.rs @@ -20,4 +20,7 @@ fn main() { foo!(a, i32); foo!(b, f32); foo!(c, f64); + + // do not lint cast to cfg-dependant type + 1 as std::os::raw::c_char; } |
