forked from use-ink/ink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Will close use-ink#2073
- Loading branch information
Showing
5 changed files
with
219 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// 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 if_chain::if_chain; | ||
use rustc_hir::Expr; | ||
use rustc_lint::{ | ||
LateContext, | ||
LateLintPass, | ||
}; | ||
use rustc_session::{ | ||
declare_lint, | ||
declare_lint_pass, | ||
}; | ||
|
||
declare_lint! { | ||
/// ## What it does | ||
/// Highlights arithmetic expressions in which division is performed before multiplication. | ||
/// | ||
/// ## Why is this bad? | ||
/// Integer division might unexpectedly result with 0. | ||
/// | ||
/// ## Example | ||
/// Bad: | ||
/// | ||
/// ```rust | ||
/// let a: u32 = 2; | ||
/// let b: u32 = 4; | ||
/// let c: u32 = a / b * 8; // c == 0 | ||
/// ``` | ||
/// | ||
/// Use the following: | ||
/// | ||
/// ```rust | ||
/// let a: u32 = 2; | ||
/// let b: u32 = 4; | ||
/// let c: u32 = a * 8 / b; // c == 4 | ||
/// ``` | ||
/// | ||
pub DIVIDE_BEFORE_MULTIPLY, | ||
Warn, | ||
"division before multiplication" | ||
} | ||
|
||
declare_lint_pass!(DivideBeforeMultiply => [DIVIDE_BEFORE_MULTIPLY]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for DivideBeforeMultiply { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||
todo!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#![cfg_attr(not(feature = "std"), no_main)] | ||
|
||
pub type TyAlias1 = u32; | ||
pub type TyAlias2 = TyAlias1; | ||
|
||
#[ink::contract] | ||
pub mod divide_before_multiply { | ||
|
||
const CONST_1: u32 = 1; | ||
const CONST_2: u32 = 2; | ||
const CONST_3: u32 = 3; | ||
|
||
#[ink(storage)] | ||
pub struct DivideBeforeMultiply {} | ||
|
||
impl DivideBeforeMultiply { | ||
#[ink(constructor)] | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
fn return_1(&self) -> u32 { | ||
1 | ||
} | ||
|
||
fn return_2(&self) -> u32 { | ||
2 | ||
} | ||
|
||
#[ink(message)] | ||
pub fn test_lint(&mut self) { | ||
// Constants | ||
let _ = CONST_1 / CONST_2 * CONST_3; | ||
|
||
// Variables | ||
let a: u32 = 1; | ||
let b: u32 = 2; | ||
let _ = a / b * (4 + 4); | ||
let _ = a / b * CONST_3; | ||
let _ = a / b * a; | ||
|
||
// Function calls | ||
let _ = self.return_1() / self.return_2() * self.return_1(); | ||
let _ = a / self.return_2() * 2; | ||
let _ = self.return_1() / CONST_1 * b; | ||
|
||
// Type aliases | ||
let c: crate::TyAlias2 = 4; | ||
let _ = c / b * 4; | ||
let _ = a / c * CONST_3; | ||
let _ = a / b * c; | ||
} | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#![cfg_attr(not(feature = "std"), no_main)] | ||
|
||
pub type TyAlias1 = u32; | ||
pub type TyAlias2 = TyAlias1; | ||
|
||
#[ink::contract] | ||
pub mod divide_before_multiply { | ||
|
||
const CONST_1: u32 = 1; | ||
const CONST_2: u32 = 2; | ||
const CONST_3: u32 = 3; | ||
|
||
#[ink(storage)] | ||
pub struct DivideBeforeMultiply {} | ||
|
||
impl DivideBeforeMultiply { | ||
#[ink(constructor)] | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
|
||
fn return_1(&self) -> u32 { | ||
1 | ||
} | ||
|
||
fn return_2(&self) -> u32 { | ||
2 | ||
} | ||
|
||
#[ink(message)] | ||
pub fn test_lint(&mut self) { | ||
// Constants | ||
let _ = CONST_1 * CONST_3 / CONST_2; | ||
let _ = CONST_1 / (CONST_2 * CONST_3); | ||
|
||
// Variables | ||
let a: u32 = 1; | ||
let b: u32 = 2; | ||
let _ = a * (4 + 4) / b; | ||
let _ = a * CONST_3 / b; | ||
let _ = a * a / b; | ||
let _ = a / (b * (4 + 4)); | ||
let _ = a / (b * CONST_3); | ||
let _ = a / (b * a); | ||
|
||
// Function calls | ||
let _ = self.return_1() * self.return_1() / self.return_2(); | ||
let _ = a * 2 / self.return_2(); | ||
let _ = self.return_1() * b / CONST_1; | ||
let _ = self.return_1() / (self.return_2() * self.return_1()); | ||
let _ = a / (self.return_2() * 2); | ||
let _ = self.return_1() / (CONST_1 * b); | ||
|
||
// Type aliases | ||
let c: crate::TyAlias2 = 4; | ||
let _ = c * 4 / b; | ||
let _ = a * CONST_3 / c; | ||
let _ = a * c / b; | ||
let _ = c / (b * 4); | ||
let _ = a / (c * CONST_3); | ||
let _ = a / (b * c); | ||
|
||
// Test if suppressions work | ||
#[cfg_attr(dylint_lib = "ink_linting", allow(divide_before_multiply))] | ||
let _ = a / b * c; | ||
} | ||
} | ||
} | ||
|
||
fn main() {} |