From 75dab17ec382d582f2475a95783ad642cd2eed54 Mon Sep 17 00:00:00 2001 From: ascjones Date: Tue, 5 Jul 2022 16:55:47 +0100 Subject: [PATCH 1/7] Remove deprecated rustfmt configuration options --- .rustfmt.toml | 3 --- ink_linting/.rustfmt.toml | 3 --- 2 files changed, 6 deletions(-) delete mode 100644 ink_linting/.rustfmt.toml diff --git a/.rustfmt.toml b/.rustfmt.toml index 422da2bea..536703e6b 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -9,7 +9,6 @@ format_code_in_doc_comments = false comment_width = 80 normalize_comments = true # changed normalize_doc_attributes = false -license_template_path = "FILE_HEADER" # changed format_strings = false format_macro_matchers = false format_macro_bodies = true @@ -57,8 +56,6 @@ skip_children = false hide_parse_errors = false error_on_line_overflow = false error_on_unformatted = false -report_todo = "Always" -report_fixme = "Always" ignore = [] # Below are `rustfmt` internal settings diff --git a/ink_linting/.rustfmt.toml b/ink_linting/.rustfmt.toml deleted file mode 100644 index 8ebf443f8..000000000 --- a/ink_linting/.rustfmt.toml +++ /dev/null @@ -1,3 +0,0 @@ -license_template_path = "../FILE_HEADER" # changed -report_todo = "Always" -report_fixme = "Always" From 2d9a3234553f3858ad5a8fb13918e07e5003df8f Mon Sep 17 00:00:00 2001 From: ascjones Date: Tue, 5 Jul 2022 17:04:35 +0100 Subject: [PATCH 2/7] Fmt ink_linting --- ink_linting/src/lib.rs | 5 +- ink_linting/src/mapping_initialized.rs | 67 +++++++++++++------ .../ui/fail/mapping-nested-initialize-call.rs | 5 +- .../ui/fail/mapping-one-constructor.rs | 5 +- .../ui/fail/mapping-two-constructors-01.rs | 5 +- .../ui/fail/mapping-two-constructors-02.rs | 5 +- .../mapping-additional-logic-constructor.rs | 5 +- .../mapping-dont-use-fully-qualified-path.rs | 5 +- .../ui/pass/mapping-one-constructor.rs | 5 +- .../ui/pass/mapping-two-constructors.rs | 5 +- 10 files changed, 82 insertions(+), 30 deletions(-) diff --git a/ink_linting/src/lib.rs b/ink_linting/src/lib.rs index 7253469e5..776e46fc8 100644 --- a/ink_linting/src/lib.rs +++ b/ink_linting/src/lib.rs @@ -29,7 +29,10 @@ mod mapping_initialized; #[doc(hidden)] #[no_mangle] -pub fn register_lints(_sess: &rustc_session::Session, lint_store: &mut rustc_lint::LintStore) { +pub fn register_lints( + _sess: &rustc_session::Session, + lint_store: &mut rustc_lint::LintStore, +) { lint_store.register_lints(&[mapping_initialized::MAPPING_INITIALIZED]); lint_store.register_late_pass(|| Box::new(mapping_initialized::MappingInitialized)); } diff --git a/ink_linting/src/mapping_initialized.rs b/ink_linting/src/mapping_initialized.rs index cd723225d..94fdfc678 100644 --- a/ink_linting/src/mapping_initialized.rs +++ b/ink_linting/src/mapping_initialized.rs @@ -14,20 +14,42 @@ // You should have received a copy of the GNU General Public License // along with cargo-contract. If not, see . -use clippy_utils::diagnostics::span_lint_and_then; -use clippy_utils::source::snippet_opt; +use clippy_utils::{ + diagnostics::span_lint_and_then, + source::snippet_opt, +}; use if_chain::if_chain; use regex::Regex; use rustc_errors::Applicability; use rustc_hir::{ def_id::DefId, - intravisit::{walk_fn, walk_item, walk_qpath, FnKind, Visitor}, - BodyId, FnDecl, HirId, Item, ItemKind, + intravisit::{ + walk_fn, + walk_item, + walk_qpath, + FnKind, + Visitor, + }, + BodyId, + FnDecl, + HirId, + Item, + ItemKind, + QPath, + VariantData, +}; +use rustc_lint::{ + LateContext, + LateLintPass, +}; +use rustc_middle::{ + hir::nested_filter, + ty::Attributes, +}; +use rustc_session::{ + declare_lint, + declare_lint_pass, }; -use rustc_hir::{QPath, VariantData}; -use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::{hir::nested_filter, ty::Attributes}; -use rustc_session::{declare_lint, declare_lint_pass}; use rustc_span::source_map::Span; declare_lint! { @@ -123,12 +145,12 @@ impl<'tcx> LateLintPass<'tcx> for MappingInitialized { let attrs = cx.tcx.hir().attrs(item.hir_id()); let ink_attrs = get_ink_attribute(attrs); if ink_attrs.is_none() { - return; + return } if let ItemKind::Struct(variant_data, _) = &item.kind { if let VariantData::Unit(..) = variant_data { - return; + return } check_struct(cx, item, variant_data); } @@ -156,14 +178,14 @@ fn check_struct<'a>(cx: &LateContext<'a>, item: &'a Item, data: &VariantData) { .expect("failed creating regex"); if re.is_match(&format!("{:?}", field.ty.kind)) { marker = Some(field); - return true; + return true } false }); if !storage_contains_mapping { log::debug!("Found `#[ink(storage)]` struct without `Mapping`"); - return; + return } log::debug!("Found `#[ink(storage)]` struct with `Mapping`"); @@ -257,7 +279,7 @@ impl<'tcx> Visitor<'tcx> for InkAttributeVisitor<'_, 'tcx> { // We can return immediately if an incorrect constructor was already found if let Some(constructor) = &self.constructor_info { if !constructor.uses_initialize_contract { - return; + return } } @@ -265,9 +287,11 @@ impl<'tcx> Visitor<'tcx> for InkAttributeVisitor<'_, 'tcx> { self.ink_attribute = get_ink_attribute(attrs); if self.ink_attribute == Some(InkAttribute::Storage) { - return; + return } else if self.ink_attribute == Some(InkAttribute::Constructor) { - log::debug!("Found constructor, starting to search for `initialize_contract`"); + log::debug!( + "Found constructor, starting to search for `initialize_contract`" + ); let mut visitor = InitializeContractVisitor { cx: self.cx, uses_initialize_contract: false, @@ -283,7 +307,7 @@ impl<'tcx> Visitor<'tcx> for InkAttributeVisitor<'_, 'tcx> { span, }); - return; + return } walk_fn(self, kind, decl, body_id, span, id); @@ -313,17 +337,18 @@ impl<'tcx> Visitor<'tcx> for InitializeContractVisitor<'_, 'tcx> { fn visit_qpath(&mut self, qpath: &'tcx QPath<'_>, id: HirId, span: Span) { log::debug!("Visiting path {:?}", qpath); if self.uses_initialize_contract { - return; + return } if let QPath::Resolved(_, path) = qpath { log::debug!("QPath: {:?}", path.res); - let re = - Regex::new(r"ink_lang\[.*\]::codegen::dispatch::execution::initialize_contract") - .expect("failed creating regex"); + let re = Regex::new( + r"ink_lang\[.*\]::codegen::dispatch::execution::initialize_contract", + ) + .expect("failed creating regex"); if re.is_match(&format!("{:?}", path.res)) { self.uses_initialize_contract = true; - return; + return } } diff --git a/ink_linting/ui/fail/mapping-nested-initialize-call.rs b/ink_linting/ui/fail/mapping-nested-initialize-call.rs index a8064b724..b9086a65a 100644 --- a/ink_linting/ui/fail/mapping-nested-initialize-call.rs +++ b/ink_linting/ui/fail/mapping-nested-initialize-call.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/fail/mapping-one-constructor.rs b/ink_linting/ui/fail/mapping-one-constructor.rs index 18fbe1b78..8c6081995 100644 --- a/ink_linting/ui/fail/mapping-one-constructor.rs +++ b/ink_linting/ui/fail/mapping-one-constructor.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/fail/mapping-two-constructors-01.rs b/ink_linting/ui/fail/mapping-two-constructors-01.rs index 645ce9a50..4c2e426bf 100644 --- a/ink_linting/ui/fail/mapping-two-constructors-01.rs +++ b/ink_linting/ui/fail/mapping-two-constructors-01.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/fail/mapping-two-constructors-02.rs b/ink_linting/ui/fail/mapping-two-constructors-02.rs index 253ac1a5b..a92f4952b 100644 --- a/ink_linting/ui/fail/mapping-two-constructors-02.rs +++ b/ink_linting/ui/fail/mapping-two-constructors-02.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-additional-logic-constructor.rs b/ink_linting/ui/pass/mapping-additional-logic-constructor.rs index 964d8a91c..d57864527 100644 --- a/ink_linting/ui/pass/mapping-additional-logic-constructor.rs +++ b/ink_linting/ui/pass/mapping-additional-logic-constructor.rs @@ -22,7 +22,10 @@ use ink_lang as ink; mod my_contract { /// The `Mapping` must be recognized, even if it is imported /// under a different name. - use ink_storage::{traits::SpreadAllocate, Mapping as SomeOtherName}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping as SomeOtherName, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs b/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs index c56c189d2..c1245dc52 100644 --- a/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs +++ b/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs @@ -21,7 +21,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { use ink_lang::utils::initialize_contract as foo; - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-one-constructor.rs b/ink_linting/ui/pass/mapping-one-constructor.rs index 48544660d..382238b43 100644 --- a/ink_linting/ui/pass/mapping-one-constructor.rs +++ b/ink_linting/ui/pass/mapping-one-constructor.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-two-constructors.rs b/ink_linting/ui/pass/mapping-two-constructors.rs index f16cddfa2..4ee1dbe40 100644 --- a/ink_linting/ui/pass/mapping-two-constructors.rs +++ b/ink_linting/ui/pass/mapping-two-constructors.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] From 36d7b8c9968bd723e545077eeaac003c345b1a3c Mon Sep 17 00:00:00 2001 From: ascjones Date: Wed, 6 Jul 2022 09:44:47 +0100 Subject: [PATCH 3/7] Ignore all target dirs --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 53eaa2196..84a323e7d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/target +target **/*.rs.bk From a87e106325b3b39e2ce47f3f6ab32c4c39f73f23 Mon Sep 17 00:00:00 2001 From: ascjones Date: Wed, 6 Jul 2022 09:45:02 +0100 Subject: [PATCH 4/7] Update linting toolchain file --- ink_linting/rust-toolchain.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ink_linting/rust-toolchain.toml b/ink_linting/rust-toolchain.toml index 7a49fef43..6bcb1e0b7 100644 --- a/ink_linting/rust-toolchain.toml +++ b/ink_linting/rust-toolchain.toml @@ -2,5 +2,5 @@ # https://github.com/trailofbits/dylint/tree/master/examples. [toolchain] -channel = "nightly-2022-03-14" +channel = "nightly-2022-06-30" components = ["llvm-tools-preview", "rustc-dev"] \ No newline at end of file From 66b86220e2b11dcecafc9da5b330123fc100743c Mon Sep 17 00:00:00 2001 From: ascjones Date: Fri, 8 Jul 2022 12:53:33 +0100 Subject: [PATCH 5/7] Revert "Fmt ink_linting" This reverts commit 2d9a3234553f3858ad5a8fb13918e07e5003df8f. --- ink_linting/src/lib.rs | 5 +- ink_linting/src/mapping_initialized.rs | 67 ++++++------------- .../ui/fail/mapping-nested-initialize-call.rs | 5 +- .../ui/fail/mapping-one-constructor.rs | 5 +- .../ui/fail/mapping-two-constructors-01.rs | 5 +- .../ui/fail/mapping-two-constructors-02.rs | 5 +- .../mapping-additional-logic-constructor.rs | 5 +- .../mapping-dont-use-fully-qualified-path.rs | 5 +- .../ui/pass/mapping-one-constructor.rs | 5 +- .../ui/pass/mapping-two-constructors.rs | 5 +- 10 files changed, 30 insertions(+), 82 deletions(-) diff --git a/ink_linting/src/lib.rs b/ink_linting/src/lib.rs index 776e46fc8..7253469e5 100644 --- a/ink_linting/src/lib.rs +++ b/ink_linting/src/lib.rs @@ -29,10 +29,7 @@ mod mapping_initialized; #[doc(hidden)] #[no_mangle] -pub fn register_lints( - _sess: &rustc_session::Session, - lint_store: &mut rustc_lint::LintStore, -) { +pub fn register_lints(_sess: &rustc_session::Session, lint_store: &mut rustc_lint::LintStore) { lint_store.register_lints(&[mapping_initialized::MAPPING_INITIALIZED]); lint_store.register_late_pass(|| Box::new(mapping_initialized::MappingInitialized)); } diff --git a/ink_linting/src/mapping_initialized.rs b/ink_linting/src/mapping_initialized.rs index 94fdfc678..cd723225d 100644 --- a/ink_linting/src/mapping_initialized.rs +++ b/ink_linting/src/mapping_initialized.rs @@ -14,42 +14,20 @@ // You should have received a copy of the GNU General Public License // along with cargo-contract. If not, see . -use clippy_utils::{ - diagnostics::span_lint_and_then, - source::snippet_opt, -}; +use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::source::snippet_opt; use if_chain::if_chain; use regex::Regex; use rustc_errors::Applicability; use rustc_hir::{ def_id::DefId, - intravisit::{ - walk_fn, - walk_item, - walk_qpath, - FnKind, - Visitor, - }, - BodyId, - FnDecl, - HirId, - Item, - ItemKind, - QPath, - VariantData, -}; -use rustc_lint::{ - LateContext, - LateLintPass, -}; -use rustc_middle::{ - hir::nested_filter, - ty::Attributes, -}; -use rustc_session::{ - declare_lint, - declare_lint_pass, + intravisit::{walk_fn, walk_item, walk_qpath, FnKind, Visitor}, + BodyId, FnDecl, HirId, Item, ItemKind, }; +use rustc_hir::{QPath, VariantData}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_middle::{hir::nested_filter, ty::Attributes}; +use rustc_session::{declare_lint, declare_lint_pass}; use rustc_span::source_map::Span; declare_lint! { @@ -145,12 +123,12 @@ impl<'tcx> LateLintPass<'tcx> for MappingInitialized { let attrs = cx.tcx.hir().attrs(item.hir_id()); let ink_attrs = get_ink_attribute(attrs); if ink_attrs.is_none() { - return + return; } if let ItemKind::Struct(variant_data, _) = &item.kind { if let VariantData::Unit(..) = variant_data { - return + return; } check_struct(cx, item, variant_data); } @@ -178,14 +156,14 @@ fn check_struct<'a>(cx: &LateContext<'a>, item: &'a Item, data: &VariantData) { .expect("failed creating regex"); if re.is_match(&format!("{:?}", field.ty.kind)) { marker = Some(field); - return true + return true; } false }); if !storage_contains_mapping { log::debug!("Found `#[ink(storage)]` struct without `Mapping`"); - return + return; } log::debug!("Found `#[ink(storage)]` struct with `Mapping`"); @@ -279,7 +257,7 @@ impl<'tcx> Visitor<'tcx> for InkAttributeVisitor<'_, 'tcx> { // We can return immediately if an incorrect constructor was already found if let Some(constructor) = &self.constructor_info { if !constructor.uses_initialize_contract { - return + return; } } @@ -287,11 +265,9 @@ impl<'tcx> Visitor<'tcx> for InkAttributeVisitor<'_, 'tcx> { self.ink_attribute = get_ink_attribute(attrs); if self.ink_attribute == Some(InkAttribute::Storage) { - return + return; } else if self.ink_attribute == Some(InkAttribute::Constructor) { - log::debug!( - "Found constructor, starting to search for `initialize_contract`" - ); + log::debug!("Found constructor, starting to search for `initialize_contract`"); let mut visitor = InitializeContractVisitor { cx: self.cx, uses_initialize_contract: false, @@ -307,7 +283,7 @@ impl<'tcx> Visitor<'tcx> for InkAttributeVisitor<'_, 'tcx> { span, }); - return + return; } walk_fn(self, kind, decl, body_id, span, id); @@ -337,18 +313,17 @@ impl<'tcx> Visitor<'tcx> for InitializeContractVisitor<'_, 'tcx> { fn visit_qpath(&mut self, qpath: &'tcx QPath<'_>, id: HirId, span: Span) { log::debug!("Visiting path {:?}", qpath); if self.uses_initialize_contract { - return + return; } if let QPath::Resolved(_, path) = qpath { log::debug!("QPath: {:?}", path.res); - let re = Regex::new( - r"ink_lang\[.*\]::codegen::dispatch::execution::initialize_contract", - ) - .expect("failed creating regex"); + let re = + Regex::new(r"ink_lang\[.*\]::codegen::dispatch::execution::initialize_contract") + .expect("failed creating regex"); if re.is_match(&format!("{:?}", path.res)) { self.uses_initialize_contract = true; - return + return; } } diff --git a/ink_linting/ui/fail/mapping-nested-initialize-call.rs b/ink_linting/ui/fail/mapping-nested-initialize-call.rs index b9086a65a..a8064b724 100644 --- a/ink_linting/ui/fail/mapping-nested-initialize-call.rs +++ b/ink_linting/ui/fail/mapping-nested-initialize-call.rs @@ -20,10 +20,7 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{ - traits::SpreadAllocate, - Mapping, - }; + use ink_storage::{traits::SpreadAllocate, Mapping}; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/fail/mapping-one-constructor.rs b/ink_linting/ui/fail/mapping-one-constructor.rs index 8c6081995..18fbe1b78 100644 --- a/ink_linting/ui/fail/mapping-one-constructor.rs +++ b/ink_linting/ui/fail/mapping-one-constructor.rs @@ -20,10 +20,7 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{ - traits::SpreadAllocate, - Mapping, - }; + use ink_storage::{traits::SpreadAllocate, Mapping}; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/fail/mapping-two-constructors-01.rs b/ink_linting/ui/fail/mapping-two-constructors-01.rs index 4c2e426bf..645ce9a50 100644 --- a/ink_linting/ui/fail/mapping-two-constructors-01.rs +++ b/ink_linting/ui/fail/mapping-two-constructors-01.rs @@ -20,10 +20,7 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{ - traits::SpreadAllocate, - Mapping, - }; + use ink_storage::{traits::SpreadAllocate, Mapping}; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/fail/mapping-two-constructors-02.rs b/ink_linting/ui/fail/mapping-two-constructors-02.rs index a92f4952b..253ac1a5b 100644 --- a/ink_linting/ui/fail/mapping-two-constructors-02.rs +++ b/ink_linting/ui/fail/mapping-two-constructors-02.rs @@ -20,10 +20,7 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{ - traits::SpreadAllocate, - Mapping, - }; + use ink_storage::{traits::SpreadAllocate, Mapping}; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-additional-logic-constructor.rs b/ink_linting/ui/pass/mapping-additional-logic-constructor.rs index d57864527..964d8a91c 100644 --- a/ink_linting/ui/pass/mapping-additional-logic-constructor.rs +++ b/ink_linting/ui/pass/mapping-additional-logic-constructor.rs @@ -22,10 +22,7 @@ use ink_lang as ink; mod my_contract { /// The `Mapping` must be recognized, even if it is imported /// under a different name. - use ink_storage::{ - traits::SpreadAllocate, - Mapping as SomeOtherName, - }; + use ink_storage::{traits::SpreadAllocate, Mapping as SomeOtherName}; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs b/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs index c1245dc52..c56c189d2 100644 --- a/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs +++ b/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs @@ -21,10 +21,7 @@ use ink_lang as ink; #[ink::contract] mod my_contract { use ink_lang::utils::initialize_contract as foo; - use ink_storage::{ - traits::SpreadAllocate, - Mapping, - }; + use ink_storage::{traits::SpreadAllocate, Mapping}; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-one-constructor.rs b/ink_linting/ui/pass/mapping-one-constructor.rs index 382238b43..48544660d 100644 --- a/ink_linting/ui/pass/mapping-one-constructor.rs +++ b/ink_linting/ui/pass/mapping-one-constructor.rs @@ -20,10 +20,7 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{ - traits::SpreadAllocate, - Mapping, - }; + use ink_storage::{traits::SpreadAllocate, Mapping}; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-two-constructors.rs b/ink_linting/ui/pass/mapping-two-constructors.rs index 4ee1dbe40..f16cddfa2 100644 --- a/ink_linting/ui/pass/mapping-two-constructors.rs +++ b/ink_linting/ui/pass/mapping-two-constructors.rs @@ -20,10 +20,7 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{ - traits::SpreadAllocate, - Mapping, - }; + use ink_storage::{traits::SpreadAllocate, Mapping}; #[ink(storage)] #[derive(SpreadAllocate)] From 941e744637945fe10af3e065d80326dd6b69ee7a Mon Sep 17 00:00:00 2001 From: ascjones Date: Fri, 8 Jul 2022 13:00:24 +0100 Subject: [PATCH 6/7] Fmt ink linting --- ink_linting/src/lib.rs | 5 +- ink_linting/src/mapping_initialized.rs | 65 +++++++++++++------ .../ui/fail/mapping-nested-initialize-call.rs | 5 +- .../ui/fail/mapping-one-constructor.rs | 5 +- .../ui/fail/mapping-two-constructors-01.rs | 5 +- .../ui/fail/mapping-two-constructors-02.rs | 5 +- .../mapping-additional-logic-constructor.rs | 5 +- .../mapping-dont-use-fully-qualified-path.rs | 5 +- .../ui/pass/mapping-one-constructor.rs | 5 +- .../ui/pass/mapping-two-constructors.rs | 5 +- 10 files changed, 82 insertions(+), 28 deletions(-) diff --git a/ink_linting/src/lib.rs b/ink_linting/src/lib.rs index cc92523e0..97ec87e7f 100644 --- a/ink_linting/src/lib.rs +++ b/ink_linting/src/lib.rs @@ -30,7 +30,10 @@ mod mapping_initialized; #[doc(hidden)] #[no_mangle] -pub fn register_lints(_sess: &rustc_session::Session, lint_store: &mut rustc_lint::LintStore) { +pub fn register_lints( + _sess: &rustc_session::Session, + lint_store: &mut rustc_lint::LintStore, +) { lint_store.register_lints(&[mapping_initialized::MAPPING_INITIALIZED]); lint_store.register_late_pass(|| Box::new(mapping_initialized::MappingInitialized)); } diff --git a/ink_linting/src/mapping_initialized.rs b/ink_linting/src/mapping_initialized.rs index dfcfbe164..92e9ceaf8 100644 --- a/ink_linting/src/mapping_initialized.rs +++ b/ink_linting/src/mapping_initialized.rs @@ -14,20 +14,44 @@ // You should have received a copy of the GNU General Public License // along with cargo-contract. If not, see . -use clippy_utils::{diagnostics::span_lint_and_then, source::snippet_opt}; +use clippy_utils::{ + diagnostics::span_lint_and_then, + source::snippet_opt, +}; use if_chain::if_chain; use regex::Regex; use rustc_ast as ast; use rustc_errors::Applicability; use rustc_hir::{ def_id::DefId, - intravisit::{walk_fn, walk_item, walk_qpath, FnKind, Visitor}, - BodyId, FnDecl, HirId, Item, ItemKind, QPath, VariantData, + intravisit::{ + walk_fn, + walk_item, + walk_qpath, + FnKind, + Visitor, + }, + BodyId, + FnDecl, + HirId, + Item, + ItemKind, + QPath, + VariantData, +}; +use rustc_lint::{ + LateContext, + LateLintPass, }; -use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter; -use rustc_session::{declare_lint, declare_lint_pass}; -use rustc_span::{source_map::Span, symbol::sym}; +use rustc_session::{ + declare_lint, + declare_lint_pass, +}; +use rustc_span::{ + source_map::Span, + symbol::sym, +}; declare_lint! { /// **What it does:** Checks for ink! contracts that use @@ -122,12 +146,12 @@ impl<'tcx> LateLintPass<'tcx> for MappingInitialized { let attrs = cx.tcx.hir().attrs(item.hir_id()); let ink_attrs = get_ink_attribute(attrs); if ink_attrs.is_none() { - return; + return } if let ItemKind::Struct(variant_data, _) = &item.kind { if let VariantData::Unit(..) = variant_data { - return; + return } check_struct(cx, item, variant_data); } @@ -155,14 +179,14 @@ fn check_struct<'a>(cx: &LateContext<'a>, item: &'a Item, data: &VariantData) { .expect("failed creating regex"); if re.is_match(&format!("{:?}", field.ty.kind)) { marker = Some(field); - return true; + return true } false }); if !storage_contains_mapping { log::debug!("Found `#[ink(storage)]` struct without `Mapping`"); - return; + return } log::debug!("Found `#[ink(storage)]` struct with `Mapping`"); @@ -256,7 +280,7 @@ impl<'tcx> Visitor<'tcx> for InkAttributeVisitor<'_, 'tcx> { // We can return immediately if an incorrect constructor was already found if let Some(constructor) = &self.constructor_info { if !constructor.uses_initialize_contract { - return; + return } } @@ -269,9 +293,11 @@ impl<'tcx> Visitor<'tcx> for InkAttributeVisitor<'_, 'tcx> { self.ink_attribute = get_ink_attribute(&attrs); if self.ink_attribute == Some(InkAttribute::Storage) { - return; + return } else if self.ink_attribute == Some(InkAttribute::Constructor) { - log::debug!("Found constructor, starting to search for `initialize_contract`"); + log::debug!( + "Found constructor, starting to search for `initialize_contract`" + ); let mut visitor = InitializeContractVisitor { cx: self.cx, uses_initialize_contract: false, @@ -287,7 +313,7 @@ impl<'tcx> Visitor<'tcx> for InkAttributeVisitor<'_, 'tcx> { span, }); - return; + return } walk_fn(self, kind, decl, body_id, span, id); @@ -317,17 +343,18 @@ impl<'tcx> Visitor<'tcx> for InitializeContractVisitor<'_, 'tcx> { fn visit_qpath(&mut self, qpath: &'tcx QPath<'_>, id: HirId, span: Span) { log::debug!("Visiting path {:?}", qpath); if self.uses_initialize_contract { - return; + return } if let QPath::Resolved(_, path) = qpath { log::debug!("QPath: {:?}", path.res); - let re = - Regex::new(r"ink_lang\[.*\]::codegen::dispatch::execution::initialize_contract") - .expect("failed creating regex"); + let re = Regex::new( + r"ink_lang\[.*\]::codegen::dispatch::execution::initialize_contract", + ) + .expect("failed creating regex"); if re.is_match(&format!("{:?}", path.res)) { self.uses_initialize_contract = true; - return; + return } } diff --git a/ink_linting/ui/fail/mapping-nested-initialize-call.rs b/ink_linting/ui/fail/mapping-nested-initialize-call.rs index a8064b724..b9086a65a 100644 --- a/ink_linting/ui/fail/mapping-nested-initialize-call.rs +++ b/ink_linting/ui/fail/mapping-nested-initialize-call.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/fail/mapping-one-constructor.rs b/ink_linting/ui/fail/mapping-one-constructor.rs index 18fbe1b78..8c6081995 100644 --- a/ink_linting/ui/fail/mapping-one-constructor.rs +++ b/ink_linting/ui/fail/mapping-one-constructor.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/fail/mapping-two-constructors-01.rs b/ink_linting/ui/fail/mapping-two-constructors-01.rs index 645ce9a50..4c2e426bf 100644 --- a/ink_linting/ui/fail/mapping-two-constructors-01.rs +++ b/ink_linting/ui/fail/mapping-two-constructors-01.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/fail/mapping-two-constructors-02.rs b/ink_linting/ui/fail/mapping-two-constructors-02.rs index 253ac1a5b..a92f4952b 100644 --- a/ink_linting/ui/fail/mapping-two-constructors-02.rs +++ b/ink_linting/ui/fail/mapping-two-constructors-02.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-additional-logic-constructor.rs b/ink_linting/ui/pass/mapping-additional-logic-constructor.rs index 964d8a91c..d57864527 100644 --- a/ink_linting/ui/pass/mapping-additional-logic-constructor.rs +++ b/ink_linting/ui/pass/mapping-additional-logic-constructor.rs @@ -22,7 +22,10 @@ use ink_lang as ink; mod my_contract { /// The `Mapping` must be recognized, even if it is imported /// under a different name. - use ink_storage::{traits::SpreadAllocate, Mapping as SomeOtherName}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping as SomeOtherName, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs b/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs index c56c189d2..c1245dc52 100644 --- a/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs +++ b/ink_linting/ui/pass/mapping-dont-use-fully-qualified-path.rs @@ -21,7 +21,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { use ink_lang::utils::initialize_contract as foo; - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-one-constructor.rs b/ink_linting/ui/pass/mapping-one-constructor.rs index 48544660d..382238b43 100644 --- a/ink_linting/ui/pass/mapping-one-constructor.rs +++ b/ink_linting/ui/pass/mapping-one-constructor.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] diff --git a/ink_linting/ui/pass/mapping-two-constructors.rs b/ink_linting/ui/pass/mapping-two-constructors.rs index f16cddfa2..4ee1dbe40 100644 --- a/ink_linting/ui/pass/mapping-two-constructors.rs +++ b/ink_linting/ui/pass/mapping-two-constructors.rs @@ -20,7 +20,10 @@ use ink_lang as ink; #[ink::contract] mod my_contract { - use ink_storage::{traits::SpreadAllocate, Mapping}; + use ink_storage::{ + traits::SpreadAllocate, + Mapping, + }; #[ink(storage)] #[derive(SpreadAllocate)] From 4e2409d3f48bc182e1647c5cf750ba9754f8ad29 Mon Sep 17 00:00:00 2001 From: ascjones Date: Fri, 8 Jul 2022 13:10:55 +0100 Subject: [PATCH 7/7] Fix UI tests after fmt --- ink_linting/ui/fail/mapping-nested-initialize-call.stderr | 4 ++-- ink_linting/ui/fail/mapping-one-constructor.stderr | 4 ++-- ink_linting/ui/fail/mapping-two-constructors-01.stderr | 4 ++-- ink_linting/ui/fail/mapping-two-constructors-02.stderr | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ink_linting/ui/fail/mapping-nested-initialize-call.stderr b/ink_linting/ui/fail/mapping-nested-initialize-call.stderr index 2bce8d28d..fc9b47bb2 100644 --- a/ink_linting/ui/fail/mapping-nested-initialize-call.stderr +++ b/ink_linting/ui/fail/mapping-nested-initialize-call.stderr @@ -1,5 +1,5 @@ error: `#[ink(storage)]` on `MyContract` contains `ink_storage::Mapping` without initializing it in the contract constructor. - --> $DIR/mapping-nested-initialize-call.rs:26:5 + --> $DIR/mapping-nested-initialize-call.rs:29:5 | LL | / #[derive(SpreadAllocate)] LL | | pub struct MyContract { @@ -9,7 +9,7 @@ LL | | } | = note: `#[deny(mapping_initialized)]` on by default help: this field uses `ink_storage::Mapping` - --> $DIR/mapping-nested-initialize-call.rs:28:9 + --> $DIR/mapping-nested-initialize-call.rs:31:9 | LL | balances: Mapping, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/ink_linting/ui/fail/mapping-one-constructor.stderr b/ink_linting/ui/fail/mapping-one-constructor.stderr index c8f71934a..b78ff9ffe 100644 --- a/ink_linting/ui/fail/mapping-one-constructor.stderr +++ b/ink_linting/ui/fail/mapping-one-constructor.stderr @@ -1,5 +1,5 @@ error: `#[ink(storage)]` on `MyContract` contains `ink_storage::Mapping` without initializing it in the contract constructor. - --> $DIR/mapping-one-constructor.rs:26:5 + --> $DIR/mapping-one-constructor.rs:29:5 | LL | / #[derive(SpreadAllocate)] LL | | pub struct MyContract { @@ -9,7 +9,7 @@ LL | | } | = note: `#[deny(mapping_initialized)]` on by default help: this field uses `ink_storage::Mapping` - --> $DIR/mapping-one-constructor.rs:28:9 + --> $DIR/mapping-one-constructor.rs:31:9 | LL | balances: Mapping, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/ink_linting/ui/fail/mapping-two-constructors-01.stderr b/ink_linting/ui/fail/mapping-two-constructors-01.stderr index 013f92b92..2c291635c 100644 --- a/ink_linting/ui/fail/mapping-two-constructors-01.stderr +++ b/ink_linting/ui/fail/mapping-two-constructors-01.stderr @@ -1,5 +1,5 @@ error: `#[ink(storage)]` on `MyContract` contains `ink_storage::Mapping` without initializing it in the contract constructor. - --> $DIR/mapping-two-constructors-01.rs:26:5 + --> $DIR/mapping-two-constructors-01.rs:29:5 | LL | / #[derive(SpreadAllocate)] LL | | pub struct MyContract { @@ -9,7 +9,7 @@ LL | | } | = note: `#[deny(mapping_initialized)]` on by default help: this field uses `ink_storage::Mapping` - --> $DIR/mapping-two-constructors-01.rs:28:9 + --> $DIR/mapping-two-constructors-01.rs:31:9 | LL | balances: Mapping, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/ink_linting/ui/fail/mapping-two-constructors-02.stderr b/ink_linting/ui/fail/mapping-two-constructors-02.stderr index 0f2d582e3..8d0916f98 100644 --- a/ink_linting/ui/fail/mapping-two-constructors-02.stderr +++ b/ink_linting/ui/fail/mapping-two-constructors-02.stderr @@ -1,5 +1,5 @@ error: `#[ink(storage)]` on `MyContract` contains `ink_storage::Mapping` without initializing it in the contract constructor. - --> $DIR/mapping-two-constructors-02.rs:26:5 + --> $DIR/mapping-two-constructors-02.rs:29:5 | LL | / #[derive(SpreadAllocate)] LL | | pub struct MyContract { @@ -9,7 +9,7 @@ LL | | } | = note: `#[deny(mapping_initialized)]` on by default help: this field uses `ink_storage::Mapping` - --> $DIR/mapping-two-constructors-02.rs:28:9 + --> $DIR/mapping-two-constructors-02.rs:31:9 | LL | balances: Mapping, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^