Skip to content

Commit f93c0d2

Browse files
committed
actuall add files
1 parent 8c2047f commit f93c0d2

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

clippy_lints/src/as_conversions.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
2+
use rustc::{declare_lint_pass, declare_tool_lint};
3+
use syntax::ast::*;
4+
5+
use crate::utils::span_help_and_lint;
6+
7+
declare_clippy_lint! {
8+
/// **What it does:** Checks for usage of `as` conversions.
9+
///
10+
/// **Why is this bad?** `as` conversions will perform many kinds of
11+
/// conversions, including silently lossy conversions and dangerous coercions.
12+
/// There are cases when it's necessary to use `as`, so the lint is
13+
/// Allow by default.
14+
///
15+
/// **Known problems:** None.
16+
///
17+
/// **Example:**
18+
/// ```rust
19+
/// let a: u32 = 0;
20+
/// let p = &a as *const u32 as *mut u32;
21+
/// ```
22+
pub AS_CONVERSIONS,
23+
pedantic,
24+
"using a potentially dangerous silent `as` conversion"
25+
}
26+
27+
declare_lint_pass!(AsConversions => [AS_CONVERSIONS]);
28+
29+
impl EarlyLintPass for AsConversions {
30+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
31+
if in_external_macro(cx.sess(), expr.span) {
32+
return;
33+
}
34+
35+
if let ExprKind::Cast(_, _) = expr.kind {
36+
span_help_and_lint(
37+
cx,
38+
AS_CONVERSIONS,
39+
expr.span,
40+
"using a potentially dangerous silent `as` conversion",
41+
"consider using a safe wrapper for this conversion",
42+
);
43+
}
44+
}
45+
}

tests/ui/as_conversions.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[warn(clippy::as_conversions)]
2+
3+
fn main() {
4+
let i = 0u32 as u64;
5+
6+
let j = &i as *const u64 as *mut u64;
7+
}

tests/ui/as_conversions.stderr

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error: using a potentially dangerous silent `as` conversion
2+
--> $DIR/as_conversions.rs:4:13
3+
|
4+
LL | let i = 0u32 as u64;
5+
| ^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::as-conversions` implied by `-D warnings`
8+
= help: consider using a safe wrapper for this conversion
9+
10+
error: using a potentially dangerous silent `as` conversion
11+
--> $DIR/as_conversions.rs:6:13
12+
|
13+
LL | let j = &i as *const u64 as *mut u64;
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= help: consider using a safe wrapper for this conversion
17+
18+
error: using a potentially dangerous silent `as` conversion
19+
--> $DIR/as_conversions.rs:6:13
20+
|
21+
LL | let j = &i as *const u64 as *mut u64;
22+
| ^^^^^^^^^^^^^^^^
23+
|
24+
= help: consider using a safe wrapper for this conversion
25+
26+
error: aborting due to 3 previous errors
27+

0 commit comments

Comments
 (0)