Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated rustfmt configuration options #632

Merged
merged 8 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions ink_linting/.rustfmt.toml

This file was deleted.

5 changes: 4 additions & 1 deletion ink_linting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
67 changes: 46 additions & 21 deletions ink_linting/src/mapping_initialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,42 @@
// You should have received a copy of the GNU General Public License
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

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! {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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`");

Expand Down Expand Up @@ -257,17 +279,19 @@ 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
}
}

let attrs = self.cx.tcx.get_attrs(id.owner.to_def_id());
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,
Expand All @@ -283,7 +307,7 @@ impl<'tcx> Visitor<'tcx> for InkAttributeVisitor<'_, 'tcx> {
span,
});

return;
return
}

walk_fn(self, kind, decl, body_id, span, id);
Expand Down Expand Up @@ -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
}
}

Expand Down
5 changes: 4 additions & 1 deletion ink_linting/ui/fail/mapping-nested-initialize-call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 4 additions & 1 deletion ink_linting/ui/fail/mapping-one-constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 4 additions & 1 deletion ink_linting/ui/fail/mapping-two-constructors-01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 4 additions & 1 deletion ink_linting/ui/fail/mapping-two-constructors-02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 4 additions & 1 deletion ink_linting/ui/pass/mapping-one-constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 4 additions & 1 deletion ink_linting/ui/pass/mapping-two-constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down