Skip to content

Commit 9f95c1f

Browse files
oslfmtsmoelius
authored andcommitted
fix recommended case and filter out macro exprs
1 parent aad10bc commit 9f95c1f

File tree

6 files changed

+27
-1417
lines changed

6 files changed

+27
-1417
lines changed

crate/diffs/type_cosplay.diff

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,25 @@ diff -r ./insecure/src/lib.rs ../../../../lints/type_cosplay/ui/insecure/src/lib
1616
Only in ../../../../lints/type_cosplay/ui/insecure/src: lib.stderr
1717
Only in ../../../../lints/type_cosplay/ui: insecure-2
1818
Only in ../../../../lints/type_cosplay/ui: insecure-3
19+
Only in ../../../../lints/type_cosplay/ui: insecure-anchor
20+
diff -r ./recommended/Cargo.toml ../../../../lints/type_cosplay/ui/recommended/Cargo.toml
21+
19c19
22+
< anchor-lang = "0.20.1"
23+
---
24+
> anchor-lang = "0.25.0"
1925
diff -r ./recommended/src/lib.rs ../../../../lints/type_cosplay/ui/recommended/src/lib.rs
20-
10c10,12
26+
10c10,17
2127
< pub fn update_user(ctx: Context<UpdateUser>) -> ProgramResult {
2228
---
2329
> pub fn update_user(
2430
> ctx: Context<UpdateUser>,
2531
> ) -> anchor_lang::solana_program::entrypoint::ProgramResult {
26-
31a34,35
32+
> let account_info: &AccountInfo = ctx.accounts.user.as_ref();
33+
> let mut data = &*account_info.data.take();
34+
> let user = User::try_deserialize(&mut data).unwrap();
35+
>
36+
> msg!("User: {:?}", user.authority);
37+
31a39,40
2738
>
2839
> fn main() {}
2940
Only in ../../../../lints/type_cosplay/ui/recommended/src: lib.stderr

lints/type_cosplay/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,17 @@ struct TypeCosplay {
7373
impl<'tcx> LateLintPass<'tcx> for TypeCosplay {
7474
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
7575
if_chain! {
76+
if !expr.span.from_expansion();
7677
if let ExprKind::Call(fnc_expr, _args_exprs) = expr.kind;
7778
// TODO: is the following if statement really needed?? don't think it's ever used.
7879
// all it does is check if AccountInfo.data is referenced...but what bytes we deser
7980
// from shouldn't impact if this is a type-cosplay issue or not.
8081
// walk each argument expression and see if the data field is referenced
82+
// TODO: maybe just check arg is a byte array
8183
// if args_exprs.iter().any(|arg| {
8284
// visit_expr_no_bodies(arg, |expr| contains_data_field_reference(cx, expr))
8385
// });
84-
// get the type that the function was called on, ie X in X::deser()
86+
// get the type that the function was called on, ie X in X::call()
8587
if let ExprKind::Path(qpath) = &fnc_expr.kind;
8688
if let QPath::TypeRelative(ty, _) = qpath;
8789
if let TyKind::Path(ty_qpath) = &ty.kind;
@@ -98,8 +100,10 @@ impl<'tcx> LateLintPass<'tcx> for TypeCosplay {
98100
cx,
99101
TYPE_COSPLAY,
100102
fnc_expr.span,
101-
&format!("{} type implements the anchor_lang::Discriminator trait. If you are using #[account] to derive Discriminator, use try_deserialize() instead.",
102-
middle_ty),
103+
&format!("`{}` type implements the `Discriminator` trait. If you are attempting to deserialize\n here and `{}` is annotated with #[account] use try_deserialize() instead.",
104+
middle_ty,
105+
middle_ty
106+
),
103107
None,
104108
"otherwise, make sure you are accounting for this type's discriminator in your deserialization function"
105109
);

lints/type_cosplay/ui/insecure-anchor/src/lib.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
error: User type implements the anchor_lang::Discriminator trait. If you are using #[account] to derive Discriminator, use try_deserialize() instead.
1+
error: `User` type implements the `Discriminator` trait. If you are attempting to deserialize
2+
here and `User` is annotated with #[account] use try_deserialize() instead.
23
--> $DIR/lib.rs:13:20
34
|
45
LL | let user = User::try_from_slice(&ctx.accounts.user.data.borrow()).unwrap();

0 commit comments

Comments
 (0)