forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new lint for Option.unwrap() and Result.unwrap()
The latter is set to Allow by default. Issue rust-lang#24
- Loading branch information
1 parent
daca92c
commit 69c13f9
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use syntax::ast::*; | ||
use rustc::lint::{Context, LintPass, LintArray}; | ||
use rustc::middle::ty; | ||
|
||
use utils::{span_lint, match_def_path, walk_ty}; | ||
|
||
#[derive(Copy,Clone)] | ||
pub struct UnwrapPass; | ||
|
||
declare_lint!(pub OPTION_UNWRAP_USED, Warn, | ||
"Warn on using unwrap() on an Option value"); | ||
declare_lint!(pub RESULT_UNWRAP_USED, Allow, | ||
"Warn on using unwrap() on a Result value"); | ||
|
||
impl LintPass for UnwrapPass { | ||
fn get_lints(&self) -> LintArray { | ||
lint_array!(OPTION_UNWRAP_USED, RESULT_UNWRAP_USED) | ||
} | ||
|
||
fn check_expr(&mut self, cx: &Context, expr: &Expr) { | ||
if let ExprMethodCall(ref ident, _, ref args) = expr.node { | ||
if ident.node.name != "unwrap" { | ||
return; | ||
} | ||
if let ty::TyEnum(did, _) = walk_ty(cx.tcx.expr_ty(&*args[0])).sty { | ||
if match_def_path(cx, did.did, &["core", "option", "Option"]) { | ||
span_lint(cx, OPTION_UNWRAP_USED, expr.span, | ||
"used unwrap() on an Option value. If you don't want \ | ||
to handle the None case gracefully, consider using | ||
expect() to provide a better panic message."); | ||
} | ||
else if match_def_path(cx, did.did, &["core", "result", "Result"]) { | ||
span_lint(cx, RESULT_UNWRAP_USED, expr.span, | ||
"used unwrap() on a Result value. Graceful handling \ | ||
of Err values is preferred."); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![feature(plugin)] | ||
#![plugin(clippy)] | ||
|
||
#[deny(option_unwrap_used, result_unwrap_used)] | ||
fn main() { | ||
let opt = Some(0); | ||
let _ = opt.unwrap(); //~ERROR | ||
|
||
let res: Result<i32, ()> = Ok(0); | ||
let _ = res.unwrap(); //~ERROR | ||
} |