mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-12-21 10:33:27 +00:00
d647696c1f
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 -.-
107 lines
3.8 KiB
Rust
107 lines
3.8 KiB
Rust
use clippy_utils::diagnostics::span_lint;
|
|
use clippy_utils::{is_try, match_trait_method, paths};
|
|
use rustc_hir as hir;
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
declare_clippy_lint! {
|
|
/// ### What it does
|
|
/// Checks for unused written/read amount.
|
|
///
|
|
/// ### Why is this bad?
|
|
/// `io::Write::write(_vectored)` and
|
|
/// `io::Read::read(_vectored)` are not guaranteed to
|
|
/// process the entire buffer. They return how many bytes were processed, which
|
|
/// might be smaller
|
|
/// than a given buffer's length. If you don't need to deal with
|
|
/// partial-write/read, use
|
|
/// `write_all`/`read_exact` instead.
|
|
///
|
|
/// ### Known problems
|
|
/// Detects only common patterns.
|
|
///
|
|
/// ### Example
|
|
/// ```rust,ignore
|
|
/// use std::io;
|
|
/// fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
|
|
/// // must be `w.write_all(b"foo")?;`
|
|
/// w.write(b"foo")?;
|
|
/// Ok(())
|
|
/// }
|
|
/// ```
|
|
#[clippy::version = "pre 1.29.0"]
|
|
pub UNUSED_IO_AMOUNT,
|
|
correctness,
|
|
"unused written/read amount"
|
|
}
|
|
|
|
declare_lint_pass!(UnusedIoAmount => [UNUSED_IO_AMOUNT]);
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
|
|
fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) {
|
|
let expr = match s.kind {
|
|
hir::StmtKind::Semi(expr) | hir::StmtKind::Expr(expr) => expr,
|
|
_ => return,
|
|
};
|
|
|
|
match expr.kind {
|
|
hir::ExprKind::Match(res, _, _) if is_try(cx, expr).is_some() => {
|
|
if let hir::ExprKind::Call(func, [ref arg_0, ..]) = res.kind {
|
|
if matches!(
|
|
func.kind,
|
|
hir::ExprKind::Path(hir::QPath::LangItem(hir::LangItem::TryTraitBranch, _))
|
|
) {
|
|
check_map_error(cx, arg_0, expr);
|
|
}
|
|
} else {
|
|
check_map_error(cx, res, expr);
|
|
}
|
|
},
|
|
hir::ExprKind::MethodCall(path, _, [ref arg_0, ..], _) => match &*path.ident.as_str() {
|
|
"expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
|
|
check_map_error(cx, arg_0, expr);
|
|
},
|
|
_ => (),
|
|
},
|
|
_ => (),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn check_map_error(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>) {
|
|
let mut call = call;
|
|
while let hir::ExprKind::MethodCall(path, _, args, _) = call.kind {
|
|
if matches!(&*path.ident.as_str(), "or" | "or_else" | "ok") {
|
|
call = &args[0];
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
check_method_call(cx, call, expr);
|
|
}
|
|
|
|
fn check_method_call(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>) {
|
|
if let hir::ExprKind::MethodCall(path, _, _, _) = call.kind {
|
|
let symbol = &*path.ident.as_str();
|
|
let read_trait = match_trait_method(cx, call, &paths::IO_READ);
|
|
let write_trait = match_trait_method(cx, call, &paths::IO_WRITE);
|
|
|
|
match (read_trait, write_trait, symbol) {
|
|
(true, _, "read") => span_lint(
|
|
cx,
|
|
UNUSED_IO_AMOUNT,
|
|
expr.span,
|
|
"read amount is not handled. Use `Read::read_exact` instead",
|
|
),
|
|
(true, _, "read_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "read amount is not handled"),
|
|
(_, true, "write") => span_lint(
|
|
cx,
|
|
UNUSED_IO_AMOUNT,
|
|
expr.span,
|
|
"written amount is not handled. Use `Write::write_all` instead",
|
|
),
|
|
(_, true, "write_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "written amount is not handled"),
|
|
_ => (),
|
|
}
|
|
}
|
|
}
|