Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix #3981 #4026

Merged
merged 1 commit into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,9 @@ fn fp_ty_mantissa_nbits(typ: Ty<'_>) -> u32 {

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if in_macro(expr.span) {
return;
}
if let ExprKind::Cast(ref ex, _) = expr.node {
let (cast_from, cast_to) = (cx.tables.expr_ty(ex), cx.tables.expr_ty(expr));
lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to);
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ fn main() {
1f32 as f32;
false as bool;
&1i32 as &i32;
// macro version
macro_rules! foo {
($a:ident, $b:ident) => {
pub fn $a() -> $b {
1 as $b
}
};
}
foo!(a, i32);
foo!(b, f32);
foo!(c, f64);

// casting integer literal to float is unnecessary
100 as f32;
100 as f64;
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/cast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,19 @@ LL | false as bool;
| ^^^^^^^^^^^^^

error: casting integer literal to f32 is unnecessary
--> $DIR/cast.rs:52:5
--> $DIR/cast.rs:64:5
|
LL | 100 as f32;
| ^^^^^^^^^^ help: try: `100_f32`

error: casting integer literal to f64 is unnecessary
--> $DIR/cast.rs:53:5
--> $DIR/cast.rs:65:5
|
LL | 100 as f64;
| ^^^^^^^^^^ help: try: `100_f64`

error: casting integer literal to f64 is unnecessary
--> $DIR/cast.rs:54:5
--> $DIR/cast.rs:66:5
|
LL | 100_i32 as f64;
| ^^^^^^^^^^^^^^ help: try: `100_f64`
Expand Down