Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch pointer::cast too in cast_ptr_alignment #6557

Merged
merged 3 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1637,12 +1637,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
return;
}
if let ExprKind::Cast(ref ex, cast_to) = expr.kind {
if let TyKind::Path(QPath::Resolved(_, path)) = cast_to.kind {
if let Res::Def(_, def_id) = path.res {
if cx.tcx.has_attr(def_id, sym::cfg) || cx.tcx.has_attr(def_id, sym::cfg_attr) {
return;
}
}
if is_hir_ty_cfg_dependant(cx, cast_to) {
return;
}
let (cast_from, cast_to) = (cx.typeck_results().expr_ty(ex), cx.typeck_results().expr_ty(expr));
lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to);
Expand Down Expand Up @@ -1691,6 +1687,21 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
lint_numeric_casts(cx, expr, ex, cast_from, cast_to);
}

lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
} else if let ExprKind::MethodCall(method_path, _, args, _) = expr.kind {
if method_path.ident.name != sym!(cast) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition can also be a part of the if_chain below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm not sure if I know what you mean.

Does it look like this:

if let ExprKind::Cast(ref ex, cast_to) = expr.kind {
    ...
    lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
} else if let ExprKind::MethodCall(method_path, _, args, _) = expr.kind {
    if_chain! {
        if method_path.ident.name == sym!(cast);
        if let Some(generic_args) = method_path.args;
        if let [GenericArg::Type(cast_to)] = generic_args.args;
        // There probably is no obvious reason to do this, just to be consistent with `as` cases.
        if !is_hir_ty_cfg_dependant(cx, cast_to);
        then {
            let (cast_from, cast_to) =
                (cx.typeck_results().expr_ty(&args[0]), cx.typeck_results().expr_ty(expr));
            lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
        }
    }

or this?:

if let ExprKind::Cast(ref ex, cast_to) = expr.kind {
    ...
    lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
}

if_chain! {
    if let ExprKind::MethodCall(method_path, _, args, _) = expr.kind;
    if method_path.ident.name == sym!(cast);
    if let Some(generic_args) = method_path.args;
    if let [GenericArg::Type(cast_to)] = generic_args.args;
    // There probably is no obvious reason to do this, just to be consistent with `as` cases.
    if !is_hir_ty_cfg_dependant(cx, cast_to);
    then {
        let (cast_from, cast_to) = (cx.typeck_results().expr_ty(&args[0]), cx.typeck_results().expr_ty(expr));
        lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
    }
}

Both look better than the current one though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this in mind:

if let ExprKind::Cast(ref ex, cast_to) = expr.kind {
    ...
    lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
} else if let ExprKind::MethodCall(method_path, _, args, _) = expr.kind {
    if_chain! {
        if method_path.ident.name != sym!(cast);
        if let Some(generic_args) = method_path.args;
        if let [GenericArg::Type(cast_to)] = generic_args.args;
        // There probably is no obvious reason to do this, just to be consistent with `as` cases.
        if is_hir_ty_cfg_dependant(cx, cast_to);
        then {
            return;
        }
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, my guess was not so far away. Since there will be only one if_chain!, I think my first one is the best. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes I must have misunderstood it the first time, that's indeed better =)

return;
}
if_chain! {
if let Some(generic_args) = method_path.args;
if let [GenericArg::Type(cast_to)] = generic_args.args;
// There probably is no obvious reason to do this, just to be consistent with `as` cases.
if is_hir_ty_cfg_dependant(cx, cast_to);
then {
return;
}
}
let (cast_from, cast_to) = (cx.typeck_results().expr_ty(&args[0]), cx.typeck_results().expr_ty(expr));
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
}
}
Expand All @@ -1714,6 +1725,18 @@ fn get_numeric_literal<'e>(expr: &'e Expr<'e>) -> Option<&'e Lit> {
}
}

fn is_hir_ty_cfg_dependant(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool {
rail-rain marked this conversation as resolved.
Show resolved Hide resolved
if_chain! {
if let TyKind::Path(QPath::Resolved(_, path)) = ty.kind;
if let Res::Def(_, def_id) = path.res;
then {
cx.tcx.has_attr(def_id, sym::cfg) || cx.tcx.has_attr(def_id, sym::cfg_attr)
} else {
false
}
}
}

fn show_unnecessary_cast(cx: &LateContext<'_>, expr: &Expr<'_>, literal_str: &str, cast_from: Ty<'_>, cast_to: Ty<'_>) {
let literal_kind_name = if cast_from.is_integral() { "integer" } else { "float" };
span_lint_and_sugg(
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/cast_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ fn main() {
(&1u8 as *const u8) as *const u16;
(&mut 1u8 as *mut u8) as *mut u16;

// cast to more-strictly-aligned type, but with the `pointer::cast` function.
(&1u8 as *const u8).cast::<u16>();
rail-rain marked this conversation as resolved.
Show resolved Hide resolved
(&mut 1u8 as *mut u8).cast::<u16>();

/* These should be ok */

// not a pointer type
Expand Down
14 changes: 13 additions & 1 deletion tests/ui/cast_alignment.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,17 @@ error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1
LL | (&mut 1u8 as *mut u8) as *mut u16;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) (1 < 2 bytes)
--> $DIR/cast_alignment.rs:15:5
|
LL | (&1u8 as *const u8).cast::<u16>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1 < 2 bytes)
--> $DIR/cast_alignment.rs:16:5
|
LL | (&mut 1u8 as *mut u8).cast::<u16>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 4 previous errors