Skip to content

Commit 8d83daa

Browse files
committed
add tests for lint-6
1 parent 1e33bd5 commit 8d83daa

File tree

18 files changed

+157
-3969
lines changed

18 files changed

+157
-3969
lines changed

Diff for: lints/duplicate-mutable-accounts/Cargo.toml

+9-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ path = "ui/insecure-2/src/lib.rs"
2121
name = "secure"
2222
path = "ui/secure/src/lib.rs"
2323

24+
[[example]]
25+
name = "recommended"
26+
path = "ui/recommended/src/lib.rs"
27+
28+
[[example]]
29+
name = "recommended-2"
30+
path = "ui/recommended-2/src/lib.rs"
31+
2432
[dependencies]
2533
clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "0cb0f7636851f9fcc57085cf80197a2ef6db098f" }
2634
dylint_linting = "2.0.1"
@@ -30,8 +38,8 @@ quote = "1.0.20"
3038
solana-lints = { path = "../../crate" }
3139

3240
[dev-dependencies]
33-
dylint_testing = "2.0.1"
3441
anchor-lang = "0.24.2"
42+
dylint_testing = "2.0.1"
3543

3644
[workspace]
3745

Diff for: lints/duplicate-mutable-accounts/src/lib.rs

+22-12
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMutableAccounts {
9393

9494
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
9595
// println!("{:#?}", self);
96-
for (_, v) in self.accounts.iter() {
96+
for (_, v) in &self.accounts {
9797
if v.len() > 1 {
9898
let gen_constraints = generate_possible_expected_constraints(v);
9999

100100
for ((one, symmetric), symbols) in gen_constraints {
101-
if !(self.streams.contains(one) || self.streams.contains(symmetric)) {
101+
if !(self.streams.contains(&one) || self.streams.contains(&symmetric)) {
102102
// get spans for offending types
103103
let mut spans: Vec<Span> = Vec::new();
104104
for (sym, span) in v {
105105
if &symbols.0 == sym || &symbols.1 == sym {
106-
spans.push(span.clone());
106+
spans.push(*span);
107107
}
108108
}
109109

@@ -129,7 +129,7 @@ fn get_anchor_account_type_def_id(field: &FieldDef) -> Option<DefId> {
129129
if_chain! {
130130
if let TyKind::Path(qpath) = &field.ty.kind;
131131
if let QPath::Resolved(_, path) = qpath;
132-
if path.segments.len() > 0;
132+
if !path.segments.is_empty();
133133
if let Some(generic_args) = path.segments[0].args;
134134
if generic_args.args.len() == ANCHOR_ACCOUNT_GENERIC_ARG_COUNT;
135135
if let GenericArg::Type(hir_ty) = &generic_args.args[1];
@@ -166,7 +166,7 @@ fn split(stream: CursorRef, delimiter: TokenKind) -> Vec<TokenStream> {
166166
split_streams.push(TokenStream::new(temp.clone()));
167167
temp.clear();
168168
} else {
169-
temp.push(TreeAndSpacing::from(t.to_owned()));
169+
temp.push(TreeAndSpacing::from(t.clone()));
170170
}
171171
});
172172
split_streams.push(TokenStream::new(temp));
@@ -181,25 +181,25 @@ fn split(stream: CursorRef, delimiter: TokenKind) -> Vec<TokenStream> {
181181
/// symmetric value, e.g., `a!=b` and `b!=a`. The second field of the tuple is a tuple of symbols, which
182182
/// represent the identifiers being compared. Following the previous example, this would be `(a, b)`.
183183
fn generate_possible_expected_constraints(
184-
identical_types: &Vec<(Symbol, Span)>,
184+
identical_types: &[(Symbol, Span)],
185185
) -> Vec<((TokenStream, TokenStream), (Symbol, Symbol))> {
186-
let mut deq = VecDeque::from(identical_types.clone());
186+
let mut deq = VecDeque::from(identical_types.to_owned());
187187
let mut gen_set = Vec::new();
188188

189189
for _ in 0..deq.len() - 1 {
190190
let first = deq.pop_front().unwrap().0;
191191
// generate stream for all other values in vec
192192
for (other, _) in &deq {
193-
let stream = create_key_check_constraint_tokenstream(&first, other);
194-
let symmetric_stream = create_key_check_constraint_tokenstream(other, &first);
195-
gen_set.push(((stream, symmetric_stream), (first, other.clone())));
193+
let stream = create_key_check_constraint_tokenstream(first, *other);
194+
let symmetric_stream = create_key_check_constraint_tokenstream(*other, first);
195+
gen_set.push(((stream, symmetric_stream), (first, *other)));
196196
}
197197
}
198198
gen_set
199199
}
200200

201201
/// Returns a `TokenStream` of form: constraint = `a`.key() != `b`.key().
202-
fn create_key_check_constraint_tokenstream(a: &Symbol, b: &Symbol) -> TokenStream {
202+
fn create_key_check_constraint_tokenstream(a: Symbol, b: Symbol) -> TokenStream {
203203
// TODO: may be more efficient way to do this, since the stream is effectively fixed
204204
// and determined. Only two tokens are variable.
205205
let constraint = vec![
@@ -239,7 +239,7 @@ pub struct Streams(Vec<TokenStream>);
239239
impl Streams {
240240
/// Returns true if `self` contains `other`, by comparing if there is an
241241
/// identical `TokenStream` in `self` regardless of span.
242-
fn contains(&self, other: TokenStream) -> bool {
242+
fn contains(&self, other: &TokenStream) -> bool {
243243
self.0.iter().any(|stream| stream.eq_unspanned(&other))
244244
}
245245
}
@@ -258,3 +258,13 @@ fn insecure_2() {
258258
fn secure() {
259259
dylint_testing::ui_test_example(env!("CARGO_PKG_NAME"), "secure");
260260
}
261+
262+
#[test]
263+
fn recommended() {
264+
dylint_testing::ui_test_example(env!("CARGO_PKG_NAME"), "recommended");
265+
}
266+
267+
#[test]
268+
fn recommended_2() {
269+
dylint_testing::ui_test_example(env!("CARGO_PKG_NAME"), "recommended-2");
270+
}

0 commit comments

Comments
 (0)