2021-03-25 18:29:11 +00:00
use clippy_utils ::diagnostics ::span_lint_and_then ;
2022-01-04 22:32:02 +00:00
use clippy_utils ::macros ::root_macro_call_first_node ;
use clippy_utils ::return_ty ;
2021-03-25 18:29:11 +00:00
use clippy_utils ::ty ::is_type_diagnostic_item ;
2022-01-04 22:32:02 +00:00
use clippy_utils ::visitors ::expr_visitor_no_bodies ;
2020-09-24 12:49:22 +00:00
use rustc_hir as hir ;
2022-01-04 22:32:02 +00:00
use rustc_hir ::intravisit ::{ FnKind , Visitor } ;
2020-09-24 12:49:22 +00:00
use rustc_lint ::{ LateContext , LateLintPass } ;
use rustc_session ::{ declare_lint_pass , declare_tool_lint } ;
2020-11-05 13:29:48 +00:00
use rustc_span ::{ sym , Span } ;
2020-09-24 12:49:22 +00:00
declare_clippy_lint! {
2021-07-29 10:16:06 +00:00
/// ### What it does
/// Checks for usage of `panic!`, `unimplemented!`, `todo!`, `unreachable!` or assertions in a function of type result.
2020-09-24 12:49:22 +00:00
///
2021-07-29 10:16:06 +00:00
/// ### Why is this bad?
/// For some codebases, it is desirable for functions of type result to return an error instead of crashing. Hence panicking macros should be avoided.
2020-09-24 12:49:22 +00:00
///
2021-07-29 10:16:06 +00:00
/// ### Known problems
/// Functions called from a function returning a `Result` may invoke a panicking macro. This is not checked.
2020-09-24 12:49:22 +00:00
///
2021-07-29 10:16:06 +00:00
/// ### Example
2020-09-24 12:49:22 +00:00
/// ```rust
/// fn result_with_panic() -> Result<bool, String>
/// {
/// panic!("error");
/// }
/// ```
2020-12-20 16:19:49 +00:00
/// Use instead:
/// ```rust
/// fn result_without_panic() -> Result<bool, String> {
/// Err(String::from("error"))
/// }
/// ```
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 19:06:26 +00:00
#[ clippy::version = " 1.48.0 " ]
2020-09-24 12:49:22 +00:00
pub PANIC_IN_RESULT_FN ,
restriction ,
2020-12-20 16:19:49 +00:00
" functions of type `Result<..>` that contain `panic!()`, `todo!()`, `unreachable()`, `unimplemented()` or assertion "
2020-09-24 12:49:22 +00:00
}
declare_lint_pass! ( PanicInResultFn = > [ PANIC_IN_RESULT_FN ] ) ;
impl < ' tcx > LateLintPass < ' tcx > for PanicInResultFn {
fn check_fn (
& mut self ,
cx : & LateContext < ' tcx > ,
fn_kind : FnKind < ' tcx > ,
_ : & ' tcx hir ::FnDecl < ' tcx > ,
body : & ' tcx hir ::Body < ' tcx > ,
span : Span ,
hir_id : hir ::HirId ,
) {
2021-10-02 23:51:01 +00:00
if ! matches! ( fn_kind , FnKind ::Closure ) & & is_type_diagnostic_item ( cx , return_ty ( cx , hir_id ) , sym ::Result ) {
2020-09-24 12:49:22 +00:00
lint_impl_body ( cx , span , body ) ;
}
}
}
fn lint_impl_body < ' tcx > ( cx : & LateContext < ' tcx > , impl_span : Span , body : & ' tcx hir ::Body < ' tcx > ) {
2022-01-04 22:32:02 +00:00
let mut panics = Vec ::new ( ) ;
expr_visitor_no_bodies ( | expr | {
let Some ( macro_call ) = root_macro_call_first_node ( cx , expr ) else { return true } ;
if matches! (
2022-05-29 01:57:15 +00:00
cx . tcx . item_name ( macro_call . def_id ) . as_str ( ) ,
2022-01-04 22:32:02 +00:00
" unimplemented " | " unreachable " | " panic " | " todo " | " assert " | " assert_eq " | " assert_ne "
) {
panics . push ( macro_call . span ) ;
return false ;
}
true
} )
. visit_expr ( & body . value ) ;
2020-12-20 16:19:49 +00:00
if ! panics . is_empty ( ) {
2020-09-24 12:49:22 +00:00
span_lint_and_then (
cx ,
PANIC_IN_RESULT_FN ,
impl_span ,
2020-12-20 16:19:49 +00:00
" used `unimplemented!()`, `unreachable!()`, `todo!()`, `panic!()` or assertion in a function that returns `Result` " ,
2020-09-24 12:49:22 +00:00
move | diag | {
diag . help (
2020-12-20 16:19:49 +00:00
" `unimplemented!()`, `unreachable!()`, `todo!()`, `panic!()` or assertions should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing " ,
2020-09-24 12:49:22 +00:00
) ;
2020-12-20 16:19:49 +00:00
diag . span_note ( panics , " return Err() instead of panicking " ) ;
2020-09-24 12:49:22 +00:00
} ,
) ;
}
}