-
Notifications
You must be signed in to change notification settings - Fork 1.6k
implemented lint unnecessary_min #11951
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
Closed
Closed
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
980ace1
works for 0 and unsigned ints
6db10d7
lint works if both are known at compiletime
20257d0
added support for extrema
e38c7e2
needed to modify other test for cargo test passing
923d964
made cargo test happy
b376f46
removed redundant code
6136434
added description of lint
8d9d196
remove useless comments
FelixMaetzler 072a077
remove useless comments
FelixMaetzler 7a32e98
modified the test files bc of last commit
2d36617
got rid of get_both_as_expr
d1a10bf
used preexisting cmp method
eabe0d5
Merge branch 'master' into master
FelixMaetzler 768a615
Appease the tools: clippy, rustdoc
compiler-errors dd39d48
Don't pass lint back out of lint decorator
compiler-errors 57e692b
Rename `Handler` as `DiagCtxt`.
nnethercote 9dee081
Rename `EarlyErrorHandler` as `EarlyDiagCtxt`.
nnethercote cc9195c
Rename `ParseSess::with_span_handler` as `ParseSess::with_dcx`.
nnethercote 7a7b654
Rename many `DiagCtxt` and `EarlyDiagCtxt` locals.
nnethercote 692636f
Give `VariantData::Struct` named fields, to clairfy `recovered`.
aDotInTheVoid 67d4b98
Plumb awaitness of for loops
eholk 42695d4
Split coroutine desugaring kind from source
compiler-errors cce967e
bool->enum for ast::PatKind::Struct presence of `..`
aDotInTheVoid e4a9ba8
Remove `Session` methods that duplicate `DiagCtxt` methods.
nnethercote 9d0924f
Fix clippy's usage of Body's coroutine_kind
compiler-errors 86f3749
Make some non-diagnostic-affecting QPath::LangItem into regular qpaths
compiler-errors 03be3e7
fix: broken GitHub corner, with working dark/light
hamirmahal 6627705
refactor: use CSS vars for GitHub Corner colors
hamirmahal aa4806f
stop [`bool_comparison`]'s suggestion from consuming parentheses
J-ZhengLi 84b5070
Add check for illegal accessing known length array with a constant index
cocodery 0a84ab4
Add test for indexing_slicing_index and modify related test
cocodery 1eb82a2
fix typos in default constructed unit structs, implied bounds, ineffe…
Takashiidobe 3b2a9a4
Extend `UNNECESSARY_TO_OWNED` to handle `split`
GuillaumeGomez 2d098fe
Add ui tests for `UNNECESSARY_TO_OWNED` on `split`
GuillaumeGomez 9120c06
fix typo in infinite loop lint
Takashiidobe e7174b3
run cargo uibless
Takashiidobe c5155b4
[`question_mark`]: also trigger on `return` statements
y21 360bb44
respect comments in `question_mark`
y21 c250f69
Move uninhabited_references to nursery
TethysSvensson d44f604
Remove blyxyas from users_on_vacation
blyxyas 5dba66c
Do not consider `async { (impl IntoFuture).await }` as redundant
samueltardieu 2e18503
New lints `iter_filter_is_some` and `iter_filter_is_ok`
PartiallyUntyped b23e4bc
Added MSRV and more tests, improved wording
PartiallyUntyped 21643a3
formatting
PartiallyUntyped f208107
more tests
PartiallyUntyped ee59198
Update version attribute for 1.75 lints
xFrednet 08530ce
Changelog for Rust 1.75 :christmas_tree:
xFrednet cebb3e7
Fix typo in changelog for 1.75
xFrednet 45bb4bd
[doc_markdown]: Add "WebGL2", "WebGPU" to default `doc_valid_idents`
waywardmonkeys 154a9ab
new lint: `eager_int_transmute`
y21 69467a0
6459: Check for redundant `matches!` with `Ready`, `Pending`, `V4`, `V6`
torfsen 7a8c109
Bump nightly version -> 2023-12-28
flip1995 bf53b19
Bump Clippy version -> 0.1.77
flip1995 b38f9cd
Remove mitigations for incorrect node args
compiler-errors 35ba1eb
Merge branch 'master' into merge-conflicts
FelixMaetzler c1b19e0
Merge branch 'rust-lang:master' into merge-conflicts
FelixMaetzler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,111 @@ | ||
use std::cmp::Ordering; | ||
|
||
use super::UNNECESSARY_MIN; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
|
||
use clippy_utils::consts::{constant, Constant}; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::{clip, int_bits, unsext}; | ||
use hir::Expr; | ||
|
||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
|
||
use rustc_middle::ty; | ||
use rustc_span::Span; | ||
|
||
pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>, arg: &'tcx Expr<'_>) { | ||
if both_are_constant(cx, expr, recv, arg) { | ||
return; | ||
} | ||
one_extrema(cx, expr, recv, arg); | ||
} | ||
fn lint(cx: &LateContext<'_>, expr: &Expr<'_>, sugg: Span, other: Span) { | ||
let msg = format!( | ||
"`{}` is never greater than `{}` and has therefore no effect", | ||
snippet(cx, sugg, "Not yet implemented"), | ||
snippet(cx, other, "Not yet implemented") | ||
); | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_MIN, | ||
expr.span, | ||
&msg, | ||
"try", | ||
snippet(cx, sugg, "Not yet implemented").to_string(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
|
||
fn try_to_eval<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
recv: &'tcx Expr<'_>, | ||
arg: &'tcx Expr<'_>, | ||
) -> (Option<Constant<'tcx>>, Option<Constant<'tcx>>) { | ||
( | ||
(constant(cx, cx.typeck_results(), recv)), | ||
(constant(cx, cx.typeck_results(), arg)), | ||
) | ||
} | ||
#[derive(Debug)] | ||
enum Extrema { | ||
Minimum, | ||
Maximum, | ||
} | ||
fn detect_extrema<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<Extrema> { | ||
let ty = cx.typeck_results().expr_ty(expr); | ||
|
||
let cv = constant(cx, cx.typeck_results(), expr)?; | ||
|
||
match (ty.kind(), cv) { | ||
(&ty::Uint(_), Constant::Int(0)) => Some(Extrema::Minimum), | ||
(&ty::Int(ity), Constant::Int(i)) if i == unsext(cx.tcx, i128::MIN >> (128 - int_bits(cx.tcx, ity)), ity) => { | ||
Some(Extrema::Minimum) | ||
}, | ||
|
||
(&ty::Int(ity), Constant::Int(i)) if i == unsext(cx.tcx, i128::MAX >> (128 - int_bits(cx.tcx, ity)), ity) => { | ||
Some(Extrema::Maximum) | ||
}, | ||
(&ty::Uint(uty), Constant::Int(i)) if i == clip(cx.tcx, u128::MAX, uty) => Some(Extrema::Maximum), | ||
|
||
_ => None, | ||
} | ||
} | ||
fn both_are_constant<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'_>, | ||
recv: &'tcx Expr<'_>, | ||
arg: &'tcx Expr<'_>, | ||
) -> bool { | ||
let ty = cx.typeck_results().expr_ty(recv); | ||
if let (Some(left), Some(right)) = try_to_eval(cx, recv, arg) | ||
&& let Some(ord) = Constant::partial_cmp(cx.tcx, ty, &left, &right) | ||
{ | ||
let (sugg, other) = match ord { | ||
Ordering::Less => (recv.span, arg.span), | ||
Ordering::Equal | Ordering::Greater => (arg.span, recv.span), | ||
}; | ||
|
||
lint(cx, expr, sugg, other); | ||
return true; | ||
} | ||
false | ||
} | ||
fn one_extrema<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>, arg: &'tcx Expr<'_>) -> bool { | ||
if let Some(extrema) = detect_extrema(cx, recv) { | ||
match extrema { | ||
Extrema::Minimum => lint(cx, expr, recv.span, arg.span), | ||
Extrema::Maximum => lint(cx, expr, arg.span, recv.span), | ||
} | ||
return true; | ||
} else if let Some(extrema) = detect_extrema(cx, arg) { | ||
match extrema { | ||
Extrema::Minimum => lint(cx, expr, arg.span, recv.span), | ||
Extrema::Maximum => lint(cx, expr, recv.span, arg.span), | ||
} | ||
return true; | ||
} | ||
|
||
false | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can use https://doc.rust-lang.org/nightly/nightly-rustc/clippy_utils/consts/enum.Constant.html#method.int_value here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh i didnt know this method existed.
I will look into that.
Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how to implement that change.
I need to use the
ty.kind()
because i have to know how many bits the integer has.If i use your proposal, than i only know if it is signed or unsigned and i can't use the math in the arms because it gives me a
i128
if it is signed. (unsext
wantsu128
)Do i miss or misunderstand something here?
Thank you for your feedback. I appreciate it.