Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c00652c
feat(linter): strict balance equality lint
jubnzv Sep 5, 2023
422eff5
feat(lint): Handle temporary values resulted after Rvalue::Use
jubnzv Sep 8, 2023
c0c2b80
fix(lint): spans to emit diagnostics
jubnzv Sep 11, 2023
4de5d35
feat(tests): more tests
jubnzv Sep 11, 2023
e679c6f
feat(lint): Manually traverse functions in user-defined code
jubnzv Sep 12, 2023
0d2953f
feat(lint): interprocedural analysis that finds tainted returns
jubnzv Sep 13, 2023
20c34c5
fix(lint): recursive calls in interprocedural analysis
jubnzv Sep 13, 2023
233ddfa
fix(lint): false negative on `CheckedBinaryOp`
jubnzv Sep 13, 2023
1cfe0e4
feat(lint): propagation through references
jubnzv Sep 14, 2023
6512927
feat(lint): Propagate tainted values through `&mut` arguments
jubnzv Sep 15, 2023
7725462
chore(lint): docstring, comments
jubnzv Sep 15, 2023
c8434d4
feat(lint): handle comparison of references in functions
jubnzv Sep 15, 2023
62a5a35
chore(tests): comments
jubnzv Sep 15, 2023
0f99906
feat(lint+tests): updated `pass` test, fixed binop conditions
jubnzv Sep 15, 2023
3e8bde3
feat(tests): test for lint suppressions
jubnzv Sep 15, 2023
0e7bfee
Merge remote-tracking branch 'origin/master' into 1811-balance-condition
jubnzv Sep 15, 2023
46238bb
chore(tests): fmt
jubnzv Sep 15, 2023
0be39dd
chore(tests): fmt
jubnzv Sep 15, 2023
e202891
chore: Add changelog entry
jubnzv Sep 17, 2023
fc4a143
Merge remote-tracking branch 'origin/master' into 1811-balance-condition
jubnzv Oct 5, 2023
96d84e0
chore(lint): Reuse utility functions introduced in #1932
jubnzv Oct 5, 2023
5b5054a
chore: Fix changelog
jubnzv Oct 5, 2023
09f8f9b
Merge remote-tracking branch 'origin/master' into 1811-balance-condition
jubnzv Oct 25, 2023
fd358b8
chore: Fix comments
jubnzv Oct 25, 2023
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
Prev Previous commit
Next Next commit
fix(lint): spans to emit diagnostics
Previously, diagnostics did not work, since `terminator.span` is
resulted after macro expansion
  • Loading branch information
jubnzv committed Sep 11, 2023
commit c0c2b8049b7d16828b6e726b441d06c8f0e42cfb
39 changes: 30 additions & 9 deletions linting/src/strict_balance_equality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use clippy_utils::{
diagnostics::span_lint_and_then,
diagnostics::span_lint_hir_and_then,
match_def_path,
source::snippet_opt,
};
Expand Down Expand Up @@ -55,7 +55,10 @@ use rustc_session::{
declare_lint,
declare_lint_pass,
};
use rustc_span::Span;
use rustc_span::{
source_map::BytePos,
Span,
};

declare_lint! {
/// **What it does:** Looks for strict equalities with balance in ink! contracts.
Expand Down Expand Up @@ -244,7 +247,7 @@ impl<'tcx> LateLintPass<'tcx> for StrictBalanceEquality {
_kind: FnKind<'tcx>,
_decl: &'tcx FnDecl<'_>,
_body: &'tcx hir::Body<'tcx>,
_span: Span,
fn_span: Span,
fn_def_id: LocalDefId,
) {
let fn_mir = cx.tcx.optimized_mir(fn_def_id);
Expand All @@ -263,23 +266,41 @@ impl<'tcx> LateLintPass<'tcx> for StrictBalanceEquality {
if let TerminatorKind::SwitchInt { discr, .. } = &terminator.kind;
if let Some(place) = discr.place();
if tainted_locals.contains(place.local);
let span = terminator.source_info.span;
let scope = terminator.source_info.scope;
let node = fn_mir.source_scopes[scope]
.local_data
.as_ref()
.assert_crate_local()
.lint_root;
if let Some(snip) = snippet_opt(cx, span);
if let Some(op) = snip.rfind("==").or(snip.rfind("!="));
then {
let span = terminator.source_info.span;
span_lint_and_then(
let op_pos = span.lo() + BytePos(op as u32);
let sugg_span = Span::new(
op_pos,
op_pos + BytePos("==".len() as u32),
// We have to use a span different from `span`, since it is resulted after
// macro expansion and therefore cannot be used to emit diagnostics.
fn_span.ctxt(),
fn_span.parent()
);
span_lint_hir_and_then(
cx,
STRICT_BALANCE_EQUALITY,
span,
node,
sugg_span,
"dangerous strict balance equality",
|diag| {
let snippet = snippet_opt(cx, span).expect("snippet must exist");
diag.span_suggestion(
span,
sugg_span,
"consider using non-strict equality operators instead: `<`, `>`".to_string(),
snippet,
"",
Applicability::Unspecified,
);
},
)

}
}
}
Expand Down
15 changes: 15 additions & 0 deletions linting/ui/fail/strict_balance_equality.stderr
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
error: dangerous strict balance equality
--> $DIR/strict_balance_equality.rs:22:37
|
LL | if self.env().balance() == 10 { // Bad
| ^^ 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:27:22
|
LL | if value == 11 {
| ^^ help: consider using non-strict equality operators instead: `<`, `>`

error: aborting due to 2 previous errors