Skip to content

Commit

Permalink
Auto merge of #6369 - camsteffen:cast-cfg, r=Manishearth
Browse files Browse the repository at this point in the history
Disable unnecessary_cast for cfg-dependant types

changelog: Disable unnecessary_cast for cfg-dependant types

Fix  #6331
  • Loading branch information
bors committed Nov 23, 2020
2 parents 723ac0f + a39a93f commit 58a2bc4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 9 additions & 1 deletion clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/unnecessary_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 58a2bc4

Please sign in to comment.