Skip to content

Commit

Permalink
add tests for lint-6
Browse files Browse the repository at this point in the history
  • Loading branch information
oslfmt committed Jul 16, 2022
1 parent 1e33bd5 commit d0c2455
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 3,970 deletions.
10 changes: 9 additions & 1 deletion lints/duplicate-mutable-accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ path = "ui/insecure-2/src/lib.rs"
name = "secure"
path = "ui/secure/src/lib.rs"

[[example]]
name = "recommended"
path = "ui/recommended/src/lib.rs"

[[example]]
name = "recommended-2"
path = "ui/recommended-2/src/lib.rs"

[dependencies]
clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "0cb0f7636851f9fcc57085cf80197a2ef6db098f" }
dylint_linting = "2.0.1"
Expand All @@ -30,8 +38,8 @@ quote = "1.0.20"
solana-lints = { path = "../../crate" }

[dev-dependencies]
dylint_testing = "2.0.1"
anchor-lang = "0.24.2"
dylint_testing = "2.0.1"

[workspace]

Expand Down
36 changes: 23 additions & 13 deletions lints/duplicate-mutable-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ impl<'tcx> LateLintPass<'tcx> for DuplicateMutableAccounts {

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

for ((one, symmetric), symbols) in gen_constraints {
if !(self.streams.contains(one) || self.streams.contains(symmetric)) {
if !(self.streams.contains(&one) || self.streams.contains(&symmetric)) {
// get spans for offending types
let mut spans: Vec<Span> = Vec::new();
for (sym, span) in v {
if &symbols.0 == sym || &symbols.1 == sym {
spans.push(span.clone());
spans.push(*span);
}
}

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

for _ in 0..deq.len() - 1 {
let first = deq.pop_front().unwrap().0;
// generate stream for all other values in vec
for (other, _) in &deq {
let stream = create_key_check_constraint_tokenstream(&first, other);
let symmetric_stream = create_key_check_constraint_tokenstream(other, &first);
gen_set.push(((stream, symmetric_stream), (first, other.clone())));
let stream = create_key_check_constraint_tokenstream(first, *other);
let symmetric_stream = create_key_check_constraint_tokenstream(*other, first);
gen_set.push(((stream, symmetric_stream), (first, *other)));
}
}
gen_set
}

/// Returns a `TokenStream` of form: constraint = `a`.key() != `b`.key().
fn create_key_check_constraint_tokenstream(a: &Symbol, b: &Symbol) -> TokenStream {
fn create_key_check_constraint_tokenstream(a: Symbol, b: Symbol) -> TokenStream {
// TODO: may be more efficient way to do this, since the stream is effectively fixed
// and determined. Only two tokens are variable.
let constraint = vec![
Expand Down Expand Up @@ -239,8 +239,8 @@ pub struct Streams(Vec<TokenStream>);
impl Streams {
/// Returns true if `self` contains `other`, by comparing if there is an
/// identical `TokenStream` in `self` regardless of span.
fn contains(&self, other: TokenStream) -> bool {
self.0.iter().any(|stream| stream.eq_unspanned(&other))
fn contains(&self, other: &TokenStream) -> bool {
self.0.iter().any(|stream| stream.eq_unspanned(other))
}
}

Expand All @@ -258,3 +258,13 @@ fn insecure_2() {
fn secure() {
dylint_testing::ui_test_example(env!("CARGO_PKG_NAME"), "secure");
}

#[test]
fn recommended() {
dylint_testing::ui_test_example(env!("CARGO_PKG_NAME"), "recommended");
}

#[test]
fn recommended_2() {
dylint_testing::ui_test_example(env!("CARGO_PKG_NAME"), "recommended-2");
}
Loading

0 comments on commit d0c2455

Please sign in to comment.