2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-05-04 21:22:43 +00:00
|
|
|
use clippy_utils::is_lang_ctor;
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::source::snippet;
|
|
|
|
use if_chain::if_chain;
|
2021-01-15 09:56:44 +00:00
|
|
|
use rustc_errors::Applicability;
|
2021-04-22 09:31:13 +00:00
|
|
|
use rustc_hir::LangItem::{OptionSome, ResultOk};
|
2021-01-15 09:56:44 +00:00
|
|
|
use rustc_hir::{Body, Expr, ExprKind, LangItem, MatchSource, QPath};
|
2021-04-27 14:55:11 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-05-04 21:22:43 +00:00
|
|
|
use rustc_middle::ty::TyS;
|
2021-04-27 14:55:11 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-01-15 09:56:44 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### What it does
|
2021-01-15 09:56:44 +00:00
|
|
|
/// Suggests alternatives for useless applications of `?` in terminating expressions
|
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// There's no reason to use `?` to short-circuit when execution of the body will end there anyway.
|
2021-01-15 09:56:44 +00:00
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
/// ### Example
|
2021-01-15 09:56:44 +00:00
|
|
|
/// ```rust
|
|
|
|
/// struct TO {
|
|
|
|
/// magic: Option<usize>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn f(to: TO) -> Option<usize> {
|
|
|
|
/// Some(to.magic?)
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// struct TR {
|
|
|
|
/// magic: Result<usize, bool>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn g(tr: Result<TR, bool>) -> Result<usize, bool> {
|
|
|
|
/// tr.and_then(|t| Ok(t.magic?))
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// struct TO {
|
|
|
|
/// magic: Option<usize>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn f(to: TO) -> Option<usize> {
|
|
|
|
/// to.magic
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// struct TR {
|
|
|
|
/// magic: Result<usize, bool>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn g(tr: Result<TR, bool>) -> Result<usize, bool> {
|
|
|
|
/// tr.and_then(|t| t.magic)
|
|
|
|
/// }
|
|
|
|
/// ```
|
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.51.0"]
|
2021-01-15 09:56:44 +00:00
|
|
|
pub NEEDLESS_QUESTION_MARK,
|
|
|
|
complexity,
|
|
|
|
"Suggest `value.inner_option` instead of `Some(value.inner_option?)`. The same goes for `Result<T, E>`."
|
|
|
|
}
|
|
|
|
|
2021-04-27 14:55:11 +00:00
|
|
|
declare_lint_pass!(NeedlessQuestionMark => [NEEDLESS_QUESTION_MARK]);
|
2021-01-15 09:56:44 +00:00
|
|
|
|
|
|
|
impl LateLintPass<'_> for NeedlessQuestionMark {
|
|
|
|
/*
|
|
|
|
* The question mark operator is compatible with both Result<T, E> and Option<T>,
|
|
|
|
* from Rust 1.13 and 1.22 respectively.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* What do we match:
|
|
|
|
* Expressions that look like this:
|
|
|
|
* Some(option?), Ok(result?)
|
|
|
|
*
|
|
|
|
* Where do we match:
|
|
|
|
* Last expression of a body
|
|
|
|
* Return statement
|
|
|
|
* A body's value (single line closure)
|
|
|
|
*
|
|
|
|
* What do we not match:
|
|
|
|
* Implicit calls to `from(..)` on the error value
|
|
|
|
*/
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
|
2021-05-04 21:22:43 +00:00
|
|
|
if let ExprKind::Ret(Some(e)) = expr.kind {
|
|
|
|
check(cx, e);
|
2021-01-15 09:56:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_body(&mut self, cx: &LateContext<'_>, body: &'_ Body<'_>) {
|
2021-05-04 21:22:43 +00:00
|
|
|
check(cx, body.value.peel_blocks());
|
2021-01-15 09:56:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 21:22:43 +00:00
|
|
|
fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|
|
|
let inner_expr = if_chain! {
|
|
|
|
if let ExprKind::Call(path, [arg]) = &expr.kind;
|
|
|
|
if let ExprKind::Path(ref qpath) = &path.kind;
|
|
|
|
if is_lang_ctor(cx, qpath, OptionSome) || is_lang_ctor(cx, qpath, ResultOk);
|
|
|
|
if let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar) = &arg.kind;
|
|
|
|
if let ExprKind::Call(called, [inner_expr]) = &inner_expr_with_q.kind;
|
2021-05-20 09:59:34 +00:00
|
|
|
if let ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, _)) = &called.kind;
|
2021-05-04 21:22:43 +00:00
|
|
|
if expr.span.ctxt() == inner_expr.span.ctxt();
|
|
|
|
let expr_ty = cx.typeck_results().expr_ty(expr);
|
|
|
|
let inner_ty = cx.typeck_results().expr_ty(inner_expr);
|
|
|
|
if TyS::same_type(expr_ty, inner_ty);
|
|
|
|
then { inner_expr } else { return; }
|
2021-01-15 09:56:44 +00:00
|
|
|
};
|
2021-03-25 18:29:11 +00:00
|
|
|
span_lint_and_sugg(
|
2021-01-15 09:56:44 +00:00
|
|
|
cx,
|
|
|
|
NEEDLESS_QUESTION_MARK,
|
2021-05-04 21:22:43 +00:00
|
|
|
expr.span,
|
2021-03-12 14:30:50 +00:00
|
|
|
"question mark operator is useless here",
|
2021-01-15 09:56:44 +00:00
|
|
|
"try",
|
2021-03-25 18:29:11 +00:00
|
|
|
format!("{}", snippet(cx, inner_expr.span, r#""...""#)),
|
2021-01-15 09:56:44 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|