Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ef29161
make parts of rustc_typeck public
Ryan1729 Aug 3, 2020
5565c1a
run cargo dev new_lint then move transmutes_expressible_as_ptr_casts …
Ryan1729 Aug 3, 2020
0c37239
make InheritedBuilder::enter public
Ryan1729 Aug 3, 2020
b4e6e70
initial compiling version of TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS
Ryan1729 Aug 3, 2020
7061b1c
write currently failing test for transmutes_expressible_as_ptr_casts
Ryan1729 Aug 3, 2020
b4ecee9
accidentally cause an ICE by putting the TRANSMUTES_EXPRESSIBLE_AS_PT…
Ryan1729 Aug 3, 2020
1e5c14d
try putting the can_be_expressed_as_pointer_cast at the top and find …
Ryan1729 Aug 3, 2020
7a818c0
get the expected number of errors by acknowledging that other lints a…
Ryan1729 Aug 3, 2020
ab93133
address some review comments
Ryan1729 Aug 4, 2020
6fdd1db
add description to assert
Ryan1729 Aug 4, 2020
8ba4101
add documentation to functions that call `do_check` and add a test ag…
Ryan1729 Aug 6, 2020
afd4909
add extra error message to the expected stderr for transmutes_express…
Ryan1729 Aug 6, 2020
5447247
change filter to assert, and update comments
Ryan1729 Aug 6, 2020
e3170bb
add newline to transmutes_expressible_as_ptr_casts.rs
Ryan1729 Aug 6, 2020
4027f15
run ./x.py fmt
Ryan1729 Aug 6, 2020
007dc94
run clippy_dev update_lints
Ryan1729 Aug 6, 2020
32691da
run clippy_dev fmt
Ryan1729 Aug 6, 2020
d897fd2
Apply suggestions from code review
Ryan1729 Aug 6, 2020
c04c4cb
copy over *.fixed file
Ryan1729 Aug 7, 2020
48a6c21
Only add a border for the rust logo
GuillaumeGomez Aug 7, 2020
b21a3de
Don't try to use wasm intrinsics on vectors
alexcrichton Aug 9, 2020
b02bf05
update stderr for transmutes_expressible_as_ptr_casts
Ryan1729 Aug 9, 2020
0fbf450
add a test example of where transmutes_expressible_as_ptr_casts shoul…
Ryan1729 Aug 9, 2020
58b8b11
fix unary minus on usize and unused variable errors in .fixed file
Ryan1729 Aug 9, 2020
d2e7293
add allow unused_unsafe and allow dead_code
Ryan1729 Aug 9, 2020
2627eed
Avoid deleting temporary files on error
Mark-Simulacrum Aug 9, 2020
bff69c9
move const_eval error reporting logic into rustc_mir::const_eval::error
RalfJung Aug 9, 2020
fd41bde
instance: only polymorphize upvar substs
davidtwco Aug 9, 2020
a0e0575
evaluate required_consts when pushing stack frame in Miri engine
RalfJung Aug 9, 2020
1fa7203
bless MIR
RalfJung Aug 9, 2020
7dba693
Use intra-doc links
denisvasilik Aug 9, 2020
fd3851a
add test for unused erroneous const in CTFE
RalfJung Aug 10, 2020
4ed0c6a
Use existing `infcx` when emitting trait impl diagnostic
Aaron1011 Aug 10, 2020
a34bc79
Add help button
GuillaumeGomez Aug 10, 2020
d7e7271
Remove AsRef link as it is in the prelude
denisvasilik Aug 10, 2020
f260462
Remove links that are in scope
denisvasilik Aug 10, 2020
eea8581
Use intra-doc links
denisvasilik Aug 10, 2020
63d1e37
Rollup merge of #75098 - Ryan1729:clippy-pointer-cast-lint-experiment…
Dylan-DPC Aug 10, 2020
2ad7c16
Rollup merge of #75249 - GuillaumeGomez:rust-logo-border, r=Manishearth
Dylan-DPC Aug 10, 2020
992988b
Rollup merge of #75315 - Mark-Simulacrum:save-temps, r=ecstatic-morse
Dylan-DPC Aug 10, 2020
2932638
Rollup merge of #75316 - alexcrichton:fix-wasm-simd, r=oli-obk
Dylan-DPC Aug 10, 2020
1e41af3
Rollup merge of #75337 - davidtwco:polymorphization-75255-fixes, r=eddyb
Dylan-DPC Aug 10, 2020
9edec57
Rollup merge of #75339 - RalfJung:eval-required, r=oli-obk
Dylan-DPC Aug 10, 2020
dff868e
Rollup merge of #75363 - Aaron1011:fix/diag-infcx, r=lcnr
Dylan-DPC Aug 10, 2020
51ed33d
Rollup merge of #75366 - GuillaumeGomez:help-button, r=jyn514
Dylan-DPC Aug 10, 2020
d00c703
Rollup merge of #75369 - denisvasilik:intra-doc-links-core-borrow, r=…
Dylan-DPC Aug 10, 2020
0a738d4
Rollup merge of #75379 - denisvasilik:intra-docs-links-core-cmp, r=Dy…
Dylan-DPC Aug 10, 2020
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
Prev Previous commit
Next Next commit
initial compiling version of TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS
  • Loading branch information
Ryan1729 committed Aug 6, 2020
commit b4e6e7047817ac12a0330060d2e176a40809c91b
74 changes: 72 additions & 2 deletions src/tools/clippy/clippy_lints/src/transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use rustc_ast::ast;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, GenericArg, Mutability, QPath, TyKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{self, Ty};
use rustc_middle::ty::{self, cast::CastKind, Ty};
use rustc_span::DUMMY_SP;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_typeck::check::{cast::CastCheck, FnCtxt, Inherited};
use std::borrow::Cow;

declare_clippy_lint! {
Expand Down Expand Up @@ -624,7 +626,21 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
);
}
},
_ => return,
(_, _) if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) => {
span_lint(
cx,
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
e.span,
&format!(
"transmute from `{}` to `{}` which could be expressed as a pointer cast instead",
from_ty,
to_ty
)
);
},
_ => {
return
},
}
}
}
Expand Down Expand Up @@ -671,3 +687,57 @@ fn is_layout_incompatible<'tcx>(cx: &LateContext<'tcx>, from: Ty<'tcx>, to: Ty<'
false
}
}

/// Check if the the type conversion can be expressed as a pointer cast, instead of a transmute.
fn can_be_expressed_as_pointer_cast<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
use CastKind::*;
matches!(
check_cast(cx, e, from_ty, to_ty),
Some(
PtrPtrCast
| PtrAddrCast
| AddrPtrCast
| ArrayPtrCast
| FnPtrPtrCast
| FnPtrAddrCast
)
)
}

/// If a cast from from_ty to to_ty is valid, returns an Ok containing the kind of the cast.
fn check_cast<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> Option<CastKind> {
let hir_id = e.hir_id;
let local_def_id = hir_id.owner;

Inherited::build(cx.tcx, local_def_id).enter(|inherited| {
let fn_ctxt = FnCtxt::new(
&inherited,
// TODO should we try to get the correct ParamEnv?
ty::ParamEnv::empty(),
hir_id
);

// If we already have errors, we can't be sure we can pointer cast.
if fn_ctxt.errors_reported_since_creation() {
return None;
}

if let Ok(check) = CastCheck::new(
&fn_ctxt,
e,
from_ty,
to_ty,
// We won't show any error to the user, so we don't care what the span is here.
DUMMY_SP,
DUMMY_SP,
) {
check.do_check(&fn_ctxt)
.ok()
// do_check's documentation says that it might return Ok and create
// errors in the fcx instead of returing Err in some cases.
.filter(|_| !fn_ctxt.errors_reported_since_creation())
} else {
None
}
})
}