Skip to content

Commit

Permalink
actuall add files
Browse files Browse the repository at this point in the history
  • Loading branch information
basil-cow committed Nov 15, 2019
1 parent 8c2047f commit f93c0d2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
45 changes: 45 additions & 0 deletions clippy_lints/src/as_conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint};
use syntax::ast::*;

use crate::utils::span_help_and_lint;

declare_clippy_lint! {
/// **What it does:** Checks for usage of `as` conversions.
///
/// **Why is this bad?** `as` conversions will perform many kinds of
/// conversions, including silently lossy conversions and dangerous coercions.
/// There are cases when it's necessary to use `as`, so the lint is
/// Allow by default.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// let a: u32 = 0;
/// let p = &a as *const u32 as *mut u32;
/// ```
pub AS_CONVERSIONS,
pedantic,
"using a potentially dangerous silent `as` conversion"
}

declare_lint_pass!(AsConversions => [AS_CONVERSIONS]);

impl EarlyLintPass for AsConversions {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
if in_external_macro(cx.sess(), expr.span) {
return;
}

if let ExprKind::Cast(_, _) = expr.kind {
span_help_and_lint(
cx,
AS_CONVERSIONS,
expr.span,
"using a potentially dangerous silent `as` conversion",
"consider using a safe wrapper for this conversion",
);
}
}
}
7 changes: 7 additions & 0 deletions tests/ui/as_conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[warn(clippy::as_conversions)]

fn main() {
let i = 0u32 as u64;

let j = &i as *const u64 as *mut u64;
}
27 changes: 27 additions & 0 deletions tests/ui/as_conversions.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error: using a potentially dangerous silent `as` conversion
--> $DIR/as_conversions.rs:4:13
|
LL | let i = 0u32 as u64;
| ^^^^^^^^^^^
|
= note: `-D clippy::as-conversions` implied by `-D warnings`
= help: consider using a safe wrapper for this conversion

error: using a potentially dangerous silent `as` conversion
--> $DIR/as_conversions.rs:6:13
|
LL | let j = &i as *const u64 as *mut u64;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using a safe wrapper for this conversion

error: using a potentially dangerous silent `as` conversion
--> $DIR/as_conversions.rs:6:13
|
LL | let j = &i as *const u64 as *mut u64;
| ^^^^^^^^^^^^^^^^
|
= help: consider using a safe wrapper for this conversion

error: aborting due to 3 previous errors

0 comments on commit f93c0d2

Please sign in to comment.