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

Linter: Prepare the ink_linting crates for publishing #2060

Merged
merged 12 commits into from
Jan 15, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Custom signature topic in Events - #[2031](https://github.com/paritytech/ink/pull/2031)
- Linter: `non_fallible_api` lint - [#2004](https://github.com/paritytech/ink/pull/2004)
- Linter: Publish the linting crates on crates.io - [#2060](https://github.com/paritytech/ink/pull/2060)

### Fixed
- Fix the `StorageVec` type by excluding the `len_cached` field from its type info - [#2052](https://github.com/paritytech/ink/pull/2052)
Expand Down
13 changes: 13 additions & 0 deletions linting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@ resolver = "2"
members = [
"mandatory",
"extra",
"utils",
]

[workspace.package]
version = "5.0.0-rc"
jubnzv marked this conversation as resolved.
Show resolved Hide resolved
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
license = "Apache-2.0"
repository = "https://github.com/paritytech/ink"
homepage = "https://www.parity.io/"
keywords = ["parity", "blockchain", "edsl", "dylint", "linting"]

[workspace.dependencies]
ink_linting_utils = { version = "=5.0.0-rc", path = "utils" }

[workspace.metadata.dylint]
libraries = [
{ path = "mandatory" },
Expand Down
37 changes: 20 additions & 17 deletions linting/extra/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
[package]
name = "ink_linting"
version = "5.0.0-rc"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
publish = false
version.workspace = true
authors.workspace = true
edition.workspace = true

license = "Apache-2.0"
license.workspace = true
readme = "README.md"
repository = "https://github.com/paritytech/ink"
repository.workspace = true
documentation = "https://docs.rs/ink_linting"
homepage = "https://github.com/paritytech/ink"
description = "Extra linting rules for ink! smart contracts"
keywords = ["parity", "blockchain", "ink", "smart contracts", "substrate"]
homepage.workspace = true
description = "Extra ink! linting rules"
keywords.workspace = true
include = ["Cargo.toml", "*.rs", "LICENSE"]

[lib]
crate-type = ["cdylib"]

[dependencies]
clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "1d334696587ac22b3a9e651e7ac684ac9e0697b2" }
dylint_linting = "2.1.12"
if_chain = "1.0.2"
log = "0.4.14"
regex = "1.5.4"
# ink! dependencies used in the linter implementation
ink_env = { path = "../../crates/env", default-features = false }
ink_linting_utils = { workspace = true }
ink_env = { version = "=5.0.0-rc", path = "../../crates/env", default-features = false }

[dev-dependencies]
dylint_testing = "2.1.12"
# The following are ink! dependencies, they are only required for the `ui` tests.
ink = { path = "../../crates/ink", default-features = false, features = ["std"] }
ink_metadata = { path = "../../crates/metadata", default-features = false }
ink_primitives = { path = "../../crates/primitives", default-features = false }
ink_storage = { path = "../../crates/storage", default-features = false }

# The ink! dependencies used to build the `ui` tests and to compile the linting
# library with `--default-features=std` (see the `features` section bellow).
#
# These cannot be moved to the workspace level because `cargo` does not provide
# the `[[workspace.dev-dependencies]]` directive.
ink = { version = "=5.0.0-rc", path = "../../crates/ink", default-features = false, features = ["std"] }
ink_metadata = { version = "=5.0.0-rc", path = "../../crates/metadata", default-features = false }
ink_primitives = { version = "=5.0.0-rc", path = "../../crates/primitives", default-features = false }
ink_storage = { version = "=5.0.0-rc", path = "../../crates/storage", default-features = false }
scale = { package = "parity-scale-codec", version = "3.4", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"] }

Expand Down
1 change: 0 additions & 1 deletion linting/extra/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_type_ir;

mod ink_utils;
mod non_fallible_api;
mod primitive_topic;
mod storage_never_freed;
Expand Down
14 changes: 7 additions & 7 deletions linting/extra/src/non_fallible_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::ink_utils::{
use if_chain::if_chain;
use ink_linting_utils::{
clippy::{
diagnostics::span_lint_and_then,
is_lint_allowed,
match_def_path,
},
expand_unnamed_consts,
find_contract_impl_id,
};
use clippy_utils::{
diagnostics::span_lint_and_then,
is_lint_allowed,
match_def_path,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{
self as hir,
Expand Down
4 changes: 2 additions & 2 deletions linting/extra/src/primitive_topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use clippy_utils::{
use if_chain::if_chain;
use ink_linting_utils::clippy::{
diagnostics::span_lint_and_then,
is_lint_allowed,
match_def_path,
source::snippet_opt,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{
self,
Expand Down
16 changes: 8 additions & 8 deletions linting/extra/src/storage_never_freed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::ink_utils::{
use if_chain::if_chain;
use ink_linting_utils::{
clippy::{
diagnostics::span_lint_and_help,
is_lint_allowed,
match_def_path,
match_path,
},
expand_unnamed_consts,
find_contract_impl_id,
find_storage_struct,
};
use clippy_utils::{
diagnostics::span_lint_and_help,
is_lint_allowed,
match_def_path,
match_path,
};
use if_chain::if_chain;
use rustc_hir::{
self as hir,
def::{
Expand Down
14 changes: 7 additions & 7 deletions linting/extra/src/strict_balance_equality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::ink_utils::{
use if_chain::if_chain;
use ink_linting_utils::{
clippy::{
diagnostics::span_lint_hir_and_then,
match_any_def_paths,
match_def_path,
},
expand_unnamed_consts,
find_contract_impl_id,
};
use clippy_utils::{
diagnostics::span_lint_hir_and_then,
match_any_def_paths,
match_def_path,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{
self as hir,
Expand Down
38 changes: 21 additions & 17 deletions linting/mandatory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
[package]
name = "ink_linting_mandatory"
version = "5.0.0-rc"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
publish = false
version.workspace = true
authors.workspace = true
edition.workspace = true

license = "Apache-2.0"
license.workspace = true
readme = "README.md"
repository = "https://github.com/paritytech/ink"
documentation = "https://docs.rs/ink_linting"
homepage = "https://github.com/paritytech/ink"
description = "Mandatory ink! linting rules integrated in contracts' build process"
keywords = ["parity", "blockchain", "ink", "smart contracts", "substrate"]
repository.workspace = true
documentation = "https://docs.rs/ink_linting_mandatory"
homepage.workspace = true
description = "Mandatory ink! linting rules"
keywords.workspace = true
include = ["Cargo.toml", "*.rs", "LICENSE"]

[lib]
crate-type = ["cdylib"]

[dependencies]
clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "1d334696587ac22b3a9e651e7ac684ac9e0697b2" }
dylint_linting = "2.1.12"
if_chain = "1.0.2"
log = "0.4.14"
regex = "1.5.4"
ink_linting_utils = { workspace = true }

[dev-dependencies]
dylint_testing = "2.1.12"
# The following are ink! dependencies, they are only required for the `ui` tests.
ink_env = { path = "../../crates/env", default-features = false }
ink = { path = "../../crates/ink", default-features = false, features = ["std"] }
ink_metadata = { path = "../../crates/metadata", default-features = false }
ink_primitives = { path = "../../crates/primitives", default-features = false }
ink_storage = { path = "../../crates/storage", default-features = false }

# The ink! dependencies used to build the `ui` tests and to compile the linting
# library with `--default-features=std` (see the `features` section bellow).
#
# These cannot be moved to the workspace level because `cargo` does not provide
# the `[[workspace.dev-dependencies]]` directive.
ink = { version = "=5.0.0-rc", path = "../../crates/ink", default-features = false, features = ["std"] }
ink_env = { version = "=5.0.0-rc", path = "../../crates/env", default-features = false }
ink_metadata = { version = "=5.0.0-rc", path = "../../crates/metadata", default-features = false }
ink_primitives = { version = "=5.0.0-rc", path = "../../crates/primitives", default-features = false }
ink_storage = { version = "=5.0.0-rc", path = "../../crates/storage", default-features = false }
scale = { package = "parity-scale-codec", version = "3.4", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"] }

Expand Down
2 changes: 1 addition & 1 deletion linting/mandatory/src/no_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use ast::{
AttrStyle,
Crate,
};
use clippy_utils::diagnostics::span_lint_and_help;
use if_chain::if_chain;
use ink_linting_utils::clippy::diagnostics::span_lint_and_help;
use rustc_ast as ast;
use rustc_lint::{
EarlyContext,
Expand Down
21 changes: 21 additions & 0 deletions linting/utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "ink_linting_utils"
version.workspace = true
authors.workspace = true
edition.workspace = true

license.workspace = true
readme = "README.md"
repository.workspace = true
documentation = "https://docs.rs/ink_linting_utils"
homepage.workspace = true
description = "Utilities used internally in ink_linting"
keywords.workspace = true
include = ["Cargo.toml", "*.rs", "LICENSE"]

[dependencies]
if_chain = "1.0.2"
parity_clippy_utils = "0.1.73"

[package.metadata.rust-analyzer]
rustc_private = true
1 change: 1 addition & 0 deletions linting/utils/LICENSE
2 changes: 2 additions & 0 deletions linting/utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ink! linting utilities
This crate implements various utility functions used in `ink_linting` crates. It also provides access to the specific version of the `clippy_utils` crate.
34 changes: 24 additions & 10 deletions linting/extra/src/ink_utils.rs → linting/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use clippy_utils::match_def_path;
#![doc(
html_logo_url = "https://use.ink/img/crate-docs/logo.png",
html_favicon_url = "https://use.ink/crate-docs/favicon.png"
)]
#![feature(rustc_private)]
#![feature(box_patterns)]

extern crate rustc_ast;
extern crate rustc_errors;
extern crate rustc_hir;
extern crate rustc_index;
extern crate rustc_lint;
extern crate rustc_middle;
extern crate rustc_mir_dataflow;
extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_type_ir;

pub use parity_clippy_utils as clippy;

use clippy::match_def_path;
use if_chain::if_chain;
use rustc_hir::{
ExprKind,
Expand All @@ -34,10 +54,7 @@ fn has_storage_attr(cx: &LateContext, hir: HirId) -> bool {
}

/// Returns `ItemId` of the structure annotated with `#[ink(storage)]`
pub(crate) fn find_storage_struct(
cx: &LateContext,
item_ids: &[ItemId],
) -> Option<ItemId> {
pub fn find_storage_struct(cx: &LateContext, item_ids: &[ItemId]) -> Option<ItemId> {
item_ids
.iter()
.find(|&item_id| {
Expand Down Expand Up @@ -78,10 +95,7 @@ fn items_in_unnamed_const(cx: &LateContext<'_>, id: &ItemId) -> Vec<ItemId> {
}

/// Collect all the `ItemId`s in nested `const _: () = {}`
pub(crate) fn expand_unnamed_consts(
cx: &LateContext<'_>,
item_ids: &[ItemId],
) -> Vec<ItemId> {
pub fn expand_unnamed_consts(cx: &LateContext<'_>, item_ids: &[ItemId]) -> Vec<ItemId> {
item_ids.iter().fold(Vec::new(), |mut acc, item_id| {
acc.push(*item_id);
acc.append(&mut items_in_unnamed_const(cx, item_id));
Expand Down Expand Up @@ -124,7 +138,7 @@ fn eq_hir_struct_tys(lhs: &Ty<'_>, rhs: &Ty<'_>) -> bool {
}

/// Finds an ID of the implementation of the contract struct containing user-defined code
pub(crate) fn find_contract_impl_id(
pub fn find_contract_impl_id(
cx: &LateContext<'_>,
item_ids: Vec<ItemId>,
) -> Option<ItemId> {
Expand Down
Loading