Skip to content

Commit 8d3eb98

Browse files
New lint missing_mut_constraint (#11)
* feat: new lint missing_mut_constraint * chore: cargo fmt * tests: Use workspace deps for anchor in test programs * Update lints/missing_mut_constraint/Cargo.toml Co-authored-by: Jamie Hill-Daniel <134328753+jamie-osec@users.noreply.github.com> * refactor: move account_name_from_place_or_rvalue into MirAnalyzer * chore: Use `cargo-install` action to install package (#14) * chore: cargo fmt --------- Co-authored-by: Jamie Hill-Daniel <134328753+jamie-osec@users.noreply.github.com>
1 parent 7c6c01a commit 8d3eb98

22 files changed

Lines changed: 467 additions & 274 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cargo install cargo-dylint dylint-link
2828
| [`direct_lamport_cpi_dos`](lints/direct_lamport_cpi_dos) |
2929
| [`overconstrained_seed_account`](lints/overconstrained_seed_account) |
3030
| [`unsafe_pyth_price_account`](lints/unsafe_pyth_price_account) |
31+
| [`missing_mut_constraint`](lints/missing_mut_constraint) |
3132

3233
## Usage
3334

@@ -68,4 +69,5 @@ cargo test ata_should_use_init_if_needed_tests
6869
cargo test direct_lamport_cpi_dos_tests
6970
cargo test overconstrained_seed_account_tests
7071
cargo test unsafe_pyth_price_account_tests
72+
cargo test missing_mut_constraint_tests
7173
```

anchor-lints-utils/src/mir_analyzer/account_extraction.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::{source::HasSession, ty::is_type_diagnostic_item};
22
use rustc_middle::{
3-
mir::{HasLocalDecls, Local},
4-
ty::TyKind,
3+
mir::{HasLocalDecls, Local, Place, Rvalue},
4+
ty::{Mutability, TyKind},
55
};
66
use rustc_span::source_map::Spanned;
77
use rustc_span::sym;
@@ -183,6 +183,43 @@ impl<'cx, 'tcx> MirAnalyzer<'cx, 'tcx> {
183183
);
184184
account_name_and_locals.first().cloned()
185185
}
186+
187+
/// Extracts the account name from a place or rvalue
188+
pub fn account_name_from_place_or_rvalue(
189+
&self,
190+
place: &Place<'_>,
191+
rvalue: &Rvalue<'_>,
192+
) -> Option<String> {
193+
let base_local = place.local;
194+
let resolved = self.resolve_to_original_local(base_local, &mut HashSet::new());
195+
if let Some(acc) = self.extract_account_name_from_local(&resolved, true) {
196+
let name = acc
197+
.account_name
198+
.split('.')
199+
.next()
200+
.unwrap_or(&acc.account_name)
201+
.to_string();
202+
return Some(name);
203+
}
204+
205+
if let Rvalue::Ref(_, borrow_kind, ref_place) = rvalue
206+
&& borrow_kind.mutability() == Mutability::Mut
207+
{
208+
let base = ref_place.local;
209+
let resolved = self.resolve_to_original_local(base, &mut HashSet::new());
210+
if let Some(acc) = self.extract_account_name_from_local(&resolved, true) {
211+
let name = acc
212+
.account_name
213+
.split('.')
214+
.next()
215+
.unwrap_or(&acc.account_name)
216+
.to_string();
217+
return Some(name);
218+
}
219+
}
220+
221+
None
222+
}
186223
}
187224

188225
fn push_account_name_and_return(

lints/arbitrary_cpi_call/tests/test_program/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ workspace = "../../../../tests"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
anchor-lang = { git = "https://github.com/jamie-osec/anchor", rev = "939b843" }
13-
anchor-spl = { git = "https://github.com/jamie-osec/anchor", rev = "939b843" }
12+
anchor-lang = { workspace = true }
13+
anchor-spl = { workspace = true }

lints/ata_should_use_init_if_needed/tests/test_program/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ workspace = "../../../../tests"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
anchor-lang = { git = "https://github.com/jamie-osec/anchor", rev = "939b843", features = ["init-if-needed"] }
13-
anchor-spl = { git = "https://github.com/jamie-osec/anchor", rev = "939b843" }
12+
anchor-lang = { workspace = true, features = ["init-if-needed"] }
13+
anchor-spl = { workspace = true }
1414

lints/cpi_no_result/tests/test_program/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ workspace = "../../../../tests"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
anchor-lang = { git = "https://github.com/jamie-osec/anchor", rev = "939b843" }
13-
anchor-spl = { git = "https://github.com/jamie-osec/anchor", rev = "939b843" }
12+
anchor-lang = { workspace = true }
13+
anchor-spl = { workspace = true }
1414

lints/direct_lamport_cpi_dos/tests/test_program/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ workspace = "../../../../tests"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
anchor-lang = { git = "https://github.com/jamie-osec/anchor", rev = "939b843" }
13-
anchor-spl = { git = "https://github.com/jamie-osec/anchor", rev = "939b843" }
12+
anchor-lang = { workspace = true }
13+
anchor-spl = { workspace = true }
1414

lints/duplicate_mutable_accounts/tests/test_program/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ workspace = "../../../../tests"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
anchor-lang = { git = "https://github.com/jamie-osec/anchor", rev = "939b843" }
13-
anchor-spl = { git = "https://github.com/jamie-osec/anchor", rev = "939b843" }
12+
anchor-lang = { workspace = true }
13+
anchor-spl = { workspace = true }

lints/missing_account_field_init/tests/test_program/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ workspace = "../../../../tests"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
anchor-lang = { git = "https://github.com/jamie-osec/anchor.git", branch = "anchor-lint" }
13-
anchor-spl = { git = "https://github.com/jamie-osec/anchor.git", branch = "anchor-lint" }
12+
anchor-lang = { workspace = true }
13+
anchor-spl = { workspace = true }
1414

1515

lints/missing_account_reload/tests/test_program/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ workspace = "../../../../tests"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
anchor-lang = { git = "https://github.com/jamie-osec/anchor", rev = "939b843" }
12+
anchor-lang = { workspace = true }

0 commit comments

Comments
 (0)