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

Add no_main lint #2001

Merged
merged 11 commits into from
Nov 28, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [E2E] Call builders and extra gas margin option - [#1917](https://github.com/paritytech/ink/pull/1917)
- Linter: `storage_never_freed` lint - [#1932](https://github.com/paritytech/ink/pull/1932)
- Linter: `strict_balance_equality` lint - [#1914](https://github.com/paritytech/ink/pull/1914)
- Linter: `no_main` lint - [#2001](https://github.com/paritytech/ink/pull/2001)
- Clean E2E configuration parsing - [#1922](https://github.com/paritytech/ink/pull/1922)
- Make `set_code_hash` generic - [#1906](https://github.com/paritytech/ink/pull/1906)

Expand Down
3 changes: 3 additions & 0 deletions linting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ path = "ui/pass/strict_balance_equality.rs"
[[example]]
name = "strict_balance_equality_fail"
path = "ui/fail/strict_balance_equality.rs"
[[example]]
name = "no_main_pass"
path = "ui/pass/no_main.rs"

[package.metadata.rust-analyzer]
rustc_private = true
Expand Down
3 changes: 3 additions & 0 deletions linting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern crate rustc_session;
extern crate rustc_span;

mod ink_utils;
mod no_main;
mod primitive_topic;
mod storage_never_freed;
mod strict_balance_equality;
Expand All @@ -46,11 +47,13 @@ pub fn register_lints(
primitive_topic::PRIMITIVE_TOPIC,
storage_never_freed::STORAGE_NEVER_FREED,
strict_balance_equality::STRICT_BALANCE_EQUALITY,
no_main::NO_MAIN,
]);
lint_store.register_late_pass(|_| Box::new(primitive_topic::PrimitiveTopic));
lint_store.register_late_pass(|_| Box::new(storage_never_freed::StorageNeverFreed));
lint_store
.register_late_pass(|_| Box::new(strict_balance_equality::StrictBalanceEquality));
lint_store.register_early_pass(|| Box::new(no_main::NoMain));
}

#[test]
Expand Down
85 changes: 85 additions & 0 deletions linting/src/no_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (C) Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use ast::{
AttrStyle,
Crate,
};
use clippy_utils::diagnostics::span_lint_and_help;
use if_chain::if_chain;
use rustc_ast as ast;
use rustc_lint::{
EarlyContext,
EarlyLintPass,
LintContext,
};
use rustc_middle::lint::in_external_macro;
use rustc_session::{
declare_lint,
declare_lint_pass,
};
use rustc_span::sym;

declare_lint! {
/// ## What it does
/// Checks if a contract is annotated with the `no_main` inner attribute.
///
/// ## Why is this necessary?
/// Contracts must be annotated with `no_main` inner attribute when compiled for on-chain
/// execution.
///
/// ## Example
///
/// ```rust
/// // Bad: Contract does not contain the `no_main` attribute, so it cannot be compiled to Wasm
/// #![cfg_attr(not(feature = "std"), no_std)]
/// #[ink::contract]
/// mod my_contract { /* ... */ }
/// ```
///
/// Use instead:
///
/// ```rust
/// #![cfg_attr(not(feature = "std"), no_std, no_main)]
/// #[ink::contract]
/// mod my_contract { /* ... */ }
/// ```
pub NO_MAIN,
Deny,
"contract must be annotated with the `no_main` inner attribute"
}

declare_lint_pass!(NoMain => [NO_MAIN]);

impl EarlyLintPass for NoMain {
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
// `no_main` is an `Inner` attribute of `#![cfg_attr(...)]`
if krate.attrs.iter().all(|attr| {
if_chain! {
if !in_external_macro(cx.sess(), attr.span);
if let AttrStyle::Inner = attr.style;
if attr.has_name(sym::no_main);
then { false } else { true }}
}) {
span_lint_and_help(
cx,
NO_MAIN,
krate.spans.inner_span,
"contract must be annotated with the `no_main` inner attribute",
None,
"consider annotating contract with `#![cfg_attr(not(feature = \"std\"), no_std, no_main)]` or `#![no_main]`"
)
}
}
}
2 changes: 2 additions & 0 deletions linting/ui/fail/primitive_topic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(not(feature = "std"), no_main)]

pub type TyAlias1 = i32;
pub type TyAlias2 = TyAlias1;

Expand Down
8 changes: 4 additions & 4 deletions linting/ui/fail/primitive_topic.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
error: using `#[ink(topic)]` for a field with a primitive number type
--> $DIR/primitive_topic.rs:11:9
--> $DIR/primitive_topic.rs:13:9
|
LL | value_1: u8,
| ^^^^^^^^^^^ help: consider removing `#[ink(topic)]`: `value_1: u8`
|
= note: `-D primitive-topic` implied by `-D warnings`

error: using `#[ink(topic)]` for a field with a primitive number type
--> $DIR/primitive_topic.rs:14:9
--> $DIR/primitive_topic.rs:16:9
|
LL | value_2: Balance,
| ^^^^^^^^^^^^^^^^ help: consider removing `#[ink(topic)]`: `value_2: Balance`

error: using `#[ink(topic)]` for a field with a primitive number type
--> $DIR/primitive_topic.rs:17:9
--> $DIR/primitive_topic.rs:19:9
|
LL | value_3: crate::TyAlias1,
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `#[ink(topic)]`: `value_3: crate::TyAlias1`

error: using `#[ink(topic)]` for a field with a primitive number type
--> $DIR/primitive_topic.rs:20:9
--> $DIR/primitive_topic.rs:22:9
|
LL | value_4: crate::TyAlias2,
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `#[ink(topic)]`: `value_4: crate::TyAlias2`
Expand Down
1 change: 1 addition & 0 deletions linting/ui/fail/storage_never_freed.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), no_main)]
#![cfg_attr(dylint_lib = "ink_linting", deny(storage_never_freed))]
pub type MapAlias1<K, V> = ink::storage::Mapping<K, V>;
pub type MapAlias2<K, V> = MapAlias1<K, V>;
Expand Down
16 changes: 8 additions & 8 deletions linting/ui/fail/storage_never_freed.stderr
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
error: field's storage cannot be freed
--> $DIR/storage_never_freed.rs:17:9
--> $DIR/storage_never_freed.rs:18:9
|
LL | map_1: Mapping<AccountId, AccountId>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding operations to remove elements available to the user
note: the lint level is defined here
--> $DIR/storage_never_freed.rs:1:46
--> $DIR/storage_never_freed.rs:2:46
|
LL | #![cfg_attr(dylint_lib = "ink_linting", deny(storage_never_freed))]
| ^^^^^^^^^^^^^^^^^^^

error: field's storage cannot be freed
--> $DIR/storage_never_freed.rs:18:9
--> $DIR/storage_never_freed.rs:19:9
|
LL | map_2: Mapping<AccountId, AccountId>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding operations to remove elements available to the user

error: field's storage cannot be freed
--> $DIR/storage_never_freed.rs:19:9
--> $DIR/storage_never_freed.rs:20:9
|
LL | map_3: Mapping<AccountId, AccountId>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding operations to remove elements available to the user

error: field's storage cannot be freed
--> $DIR/storage_never_freed.rs:20:9
--> $DIR/storage_never_freed.rs:21:9
|
LL | map_alias: MapAlias2<AccountId, AccountId>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding operations to remove elements available to the user

error: field's storage cannot be freed
--> $DIR/storage_never_freed.rs:14:9
--> $DIR/storage_never_freed.rs:15:9
|
LL | vec_1: Vec<AccountId>,
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding operations to remove elements available to the user

error: field's storage cannot be freed
--> $DIR/storage_never_freed.rs:15:9
--> $DIR/storage_never_freed.rs:16:9
|
LL | vec_2: Vec<bool>,
| ^^^^^^^^^^^^^^^^
|
= help: consider adding operations to remove elements available to the user

error: field's storage cannot be freed
--> $DIR/storage_never_freed.rs:16:9
--> $DIR/storage_never_freed.rs:17:9
|
LL | vec_subscription: Vec<AccountId>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions linting/ui/fail/strict_balance_equality.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(not(feature = "std"), no_main)]

#[ink::contract]
pub mod strict_balance_equality {
#[ink(storage)]
Expand Down
24 changes: 12 additions & 12 deletions linting/ui/fail/strict_balance_equality.stderr
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:57:16
--> $DIR/strict_balance_equality.rs:59:16
|
LL | if self.env().balance() == 10 { /* ... */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`
|
= note: `-D strict-balance-equality` implied by `-D warnings`

error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:58:16
--> $DIR/strict_balance_equality.rs:60:16
|
LL | if value == 11 { /* ... */ }
| ^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`

error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:59:16
--> $DIR/strict_balance_equality.rs:61:16
|
LL | if self.env().balance() == threshold { /* ... */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`

error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:62:16
--> $DIR/strict_balance_equality.rs:64:16
|
LL | if self.get_balance_1() == 10 { /* ... */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`

error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:63:16
--> $DIR/strict_balance_equality.rs:65:16
|
LL | if self.get_balance_2() == 10 { /* ... */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`

error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:64:16
--> $DIR/strict_balance_equality.rs:66:16
|
LL | if self.get_balance_3() == 10 { /* ... */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`

error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:65:16
--> $DIR/strict_balance_equality.rs:67:16
|
LL | if self.get_balance_recursive(&10) == 10 { /* ... */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`

error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:69:16
--> $DIR/strict_balance_equality.rs:71:16
|
LL | if self.cmp_balance_1(&10) { /* ... */ }
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`

error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:70:16
--> $DIR/strict_balance_equality.rs:72:16
|
LL | if self.cmp_balance_2(&self.env().balance(), &threshold) { /* ... */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`

error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:71:16
--> $DIR/strict_balance_equality.rs:73:16
|
LL | if self.cmp_balance_3(self.env().balance(), threshold) { /* ... */ }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`

error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:76:16
--> $DIR/strict_balance_equality.rs:78:16
|
LL | if res_1 == 10 { /* ... */ }
| ^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`

error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:79:16
--> $DIR/strict_balance_equality.rs:81:16
|
LL | if res_2 == 10 { /* ... */ }
| ^^^^^^^^^^^ help: consider using non-strict equality operators instead: `<`, `>`
Expand Down
17 changes: 17 additions & 0 deletions linting/ui/pass/no_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

#[ink::contract]
pub mod no_main {
#[ink(storage)]
pub struct NoMain {}
impl NoMain {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}
#[ink(message)]
pub fn do_nothing(&mut self) {}
}
}

fn main() {}
2 changes: 2 additions & 0 deletions linting/ui/pass/primitive_topic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(not(feature = "std"), no_main)]

#[ink::contract]
pub mod primitive_topic {

Expand Down
1 change: 1 addition & 0 deletions linting/ui/pass/storage_never_freed.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), no_main)]
#![cfg_attr(dylint_lib = "ink_linting", deny(storage_never_freed))]
pub type MapAlias<K, V> = ink::storage::Mapping<K, V>;

Expand Down
2 changes: 2 additions & 0 deletions linting/ui/pass/strict_balance_equality.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(not(feature = "std"), no_main)]

#[ink::contract]
pub mod strict_balance_equality {
#[ink(storage)]
Expand Down