2021-03-16 00:55:45 +00:00
|
|
|
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
|
2021-03-14 23:17:44 +00:00
|
|
|
|
use clippy_utils::source::snippet_opt;
|
2018-12-29 15:04:45 +00:00
|
|
|
|
use rustc_errors::Applicability;
|
2020-01-06 16:39:50 +00:00
|
|
|
|
use rustc_hir::{Expr, ExprKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-11 11:37:08 +00:00
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-11-02 16:32:55 +00:00
|
|
|
|
use rustc_span::sym;
|
2018-08-29 12:40:00 +00:00
|
|
|
|
use std::fmt;
|
2018-08-27 13:49:54 +00:00
|
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-02 18:37:11 +00:00
|
|
|
|
/// ### What it does
|
|
|
|
|
/// Checks for usage of the `offset` pointer method with a `usize` casted to an
|
2019-03-05 16:50:33 +00:00
|
|
|
|
/// `isize`.
|
|
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
|
/// ### Why is this bad?
|
|
|
|
|
/// If we’re always increasing the pointer address, we can avoid the numeric
|
2019-03-05 16:50:33 +00:00
|
|
|
|
/// cast by using the `add` method instead.
|
|
|
|
|
///
|
2021-07-02 18:37:11 +00:00
|
|
|
|
/// ### Example
|
2019-03-05 16:50:33 +00:00
|
|
|
|
/// ```rust
|
|
|
|
|
/// let vec = vec![b'a', b'b', b'c'];
|
|
|
|
|
/// let ptr = vec.as_ptr();
|
|
|
|
|
/// let offset = 1_usize;
|
|
|
|
|
///
|
|
|
|
|
/// unsafe {
|
|
|
|
|
/// ptr.offset(offset as isize);
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// Could be written:
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// let vec = vec![b'a', b'b', b'c'];
|
|
|
|
|
/// let ptr = vec.as_ptr();
|
|
|
|
|
/// let offset = 1_usize;
|
|
|
|
|
///
|
|
|
|
|
/// unsafe {
|
|
|
|
|
/// ptr.add(offset);
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
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.30.0"]
|
2018-08-27 13:49:54 +00:00
|
|
|
|
pub PTR_OFFSET_WITH_CAST,
|
2018-08-29 12:02:26 +00:00
|
|
|
|
complexity,
|
2018-10-18 21:31:11 +00:00
|
|
|
|
"unneeded pointer offset cast"
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
|
declare_lint_pass!(PtrOffsetWithCast => [PTR_OFFSET_WITH_CAST]);
|
2018-08-27 13:49:54 +00:00
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for PtrOffsetWithCast {
|
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2018-08-29 12:40:00 +00:00
|
|
|
|
// Check if the expressions is a ptr.offset or ptr.wrapping_offset method call
|
|
|
|
|
let (receiver_expr, arg_expr, method) = match expr_as_ptr_offset_call(cx, expr) {
|
2018-08-27 13:49:54 +00:00
|
|
|
|
Some(call_arg) => call_arg,
|
|
|
|
|
None => return,
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-29 12:40:00 +00:00
|
|
|
|
// Check if the argument to the method call is a cast from usize
|
2018-08-27 13:49:54 +00:00
|
|
|
|
let cast_lhs_expr = match expr_as_cast_from_usize(cx, arg_expr) {
|
|
|
|
|
Some(cast_lhs_expr) => cast_lhs_expr,
|
|
|
|
|
None => return,
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-29 12:40:00 +00:00
|
|
|
|
let msg = format!("use of `{}` with a `usize` casted to an `isize`", method);
|
|
|
|
|
if let Some(sugg) = build_suggestion(cx, method, receiver_expr, cast_lhs_expr) {
|
2020-02-18 13:28:18 +00:00
|
|
|
|
span_lint_and_sugg(
|
2018-11-20 13:06:29 +00:00
|
|
|
|
cx,
|
|
|
|
|
PTR_OFFSET_WITH_CAST,
|
|
|
|
|
expr.span,
|
|
|
|
|
&msg,
|
|
|
|
|
"try",
|
|
|
|
|
sugg,
|
2018-11-27 14:13:57 +00:00
|
|
|
|
Applicability::MachineApplicable,
|
2018-11-20 13:06:29 +00:00
|
|
|
|
);
|
2018-08-29 12:12:22 +00:00
|
|
|
|
} else {
|
2020-02-18 13:28:18 +00:00
|
|
|
|
span_lint(cx, PTR_OFFSET_WITH_CAST, expr.span, &msg);
|
2018-08-29 12:12:22 +00:00
|
|
|
|
}
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the given expression is a cast from a usize, return the lhs of the cast
|
2020-06-25 20:41:36 +00:00
|
|
|
|
fn expr_as_cast_from_usize<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
|
2021-04-02 21:35:32 +00:00
|
|
|
|
if let ExprKind::Cast(cast_lhs_expr, _) = expr.kind {
|
|
|
|
|
if is_expr_ty_usize(cx, cast_lhs_expr) {
|
2018-08-27 13:49:54 +00:00
|
|
|
|
return Some(cast_lhs_expr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 12:40:00 +00:00
|
|
|
|
// If the given expression is a ptr::offset or ptr::wrapping_offset method call, return the
|
|
|
|
|
// receiver, the arg of the method call, and the method.
|
2020-06-25 20:41:36 +00:00
|
|
|
|
fn expr_as_ptr_offset_call<'tcx>(
|
|
|
|
|
cx: &LateContext<'tcx>,
|
2019-12-27 07:12:26 +00:00
|
|
|
|
expr: &'tcx Expr<'_>,
|
|
|
|
|
) -> Option<(&'tcx Expr<'tcx>, &'tcx Expr<'tcx>, Method)> {
|
2021-09-04 16:21:49 +00:00
|
|
|
|
if let ExprKind::MethodCall(path_segment, _, [arg_0, arg_1, ..], _) = &expr.kind {
|
|
|
|
|
if is_expr_ty_raw_ptr(cx, arg_0) {
|
2020-11-02 16:32:55 +00:00
|
|
|
|
if path_segment.ident.name == sym::offset {
|
2021-09-04 16:21:49 +00:00
|
|
|
|
return Some((arg_0, arg_1, Method::Offset));
|
2018-08-29 12:40:00 +00:00
|
|
|
|
}
|
2019-05-17 21:53:54 +00:00
|
|
|
|
if path_segment.ident.name == sym!(wrapping_offset) {
|
2021-09-04 16:21:49 +00:00
|
|
|
|
return Some((arg_0, arg_1, Method::WrappingOffset));
|
2018-08-29 12:40:00 +00:00
|
|
|
|
}
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Is the type of the expression a usize?
|
2020-06-25 20:41:36 +00:00
|
|
|
|
fn is_expr_ty_usize<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> bool {
|
2020-07-17 08:47:04 +00:00
|
|
|
|
cx.typeck_results().expr_ty(expr) == cx.tcx.types.usize
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Is the type of the expression a raw pointer?
|
2020-06-25 20:41:36 +00:00
|
|
|
|
fn is_expr_ty_raw_ptr<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> bool {
|
2020-07-17 08:47:04 +00:00
|
|
|
|
cx.typeck_results().expr_ty(expr).is_unsafe_ptr()
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
|
fn build_suggestion<'tcx>(
|
|
|
|
|
cx: &LateContext<'tcx>,
|
2018-08-29 12:40:00 +00:00
|
|
|
|
method: Method,
|
2019-12-27 07:12:26 +00:00
|
|
|
|
receiver_expr: &Expr<'_>,
|
|
|
|
|
cast_lhs_expr: &Expr<'_>,
|
2018-08-29 12:12:22 +00:00
|
|
|
|
) -> Option<String> {
|
2020-02-18 13:28:18 +00:00
|
|
|
|
let receiver = snippet_opt(cx, receiver_expr.span)?;
|
|
|
|
|
let cast_lhs = snippet_opt(cx, cast_lhs_expr.span)?;
|
2018-08-29 12:42:43 +00:00
|
|
|
|
Some(format!("{}.{}({})", receiver, method.suggestion(), cast_lhs))
|
2018-08-27 13:49:54 +00:00
|
|
|
|
}
|
2018-08-29 12:40:00 +00:00
|
|
|
|
|
2018-08-29 13:01:05 +00:00
|
|
|
|
#[derive(Copy, Clone)]
|
2018-08-29 12:40:00 +00:00
|
|
|
|
enum Method {
|
|
|
|
|
Offset,
|
|
|
|
|
WrappingOffset,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Method {
|
2019-09-18 06:37:41 +00:00
|
|
|
|
#[must_use]
|
2018-08-29 13:27:32 +00:00
|
|
|
|
fn suggestion(self) -> &'static str {
|
2018-08-29 13:59:38 +00:00
|
|
|
|
match self {
|
2019-07-31 00:25:35 +00:00
|
|
|
|
Self::Offset => "add",
|
|
|
|
|
Self::WrappingOffset => "wrapping_add",
|
2018-08-29 12:40:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Method {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2018-08-29 13:59:38 +00:00
|
|
|
|
match self {
|
2019-07-31 00:25:35 +00:00
|
|
|
|
Self::Offset => write!(f, "offset"),
|
|
|
|
|
Self::WrappingOffset => write!(f, "wrapping_offset"),
|
2018-08-29 12:40:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-29 12:43:40 +00:00
|
|
|
|
}
|