mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-12-13 06:42:46 +00:00
document MinifyingSugg
and Offset
...and also swap their position
This commit is contained in:
parent
ec94bd6cb4
commit
388384177e
1 changed files with 44 additions and 38 deletions
|
@ -805,44 +805,10 @@ fn same_var<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, var: HirId) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
/// a wrapper of `Sugg`. Besides what `Sugg` do, this removes unnecessary `0`;
|
||||||
enum OffsetSign {
|
/// and also, it avoids subtracting a variable from the same one by replacing it with `0`.
|
||||||
Positive,
|
/// it exists for the convenience of the overloaded operators while normal functions can do the
|
||||||
Negative,
|
/// same.
|
||||||
}
|
|
||||||
|
|
||||||
struct Offset {
|
|
||||||
value: MinifyingSugg<'static>,
|
|
||||||
sign: OffsetSign,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Offset {
|
|
||||||
fn negative(value: Sugg<'static>) -> Self {
|
|
||||||
Self {
|
|
||||||
value: value.into(),
|
|
||||||
sign: OffsetSign::Negative,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn positive(value: Sugg<'static>) -> Self {
|
|
||||||
Self {
|
|
||||||
value: value.into(),
|
|
||||||
sign: OffsetSign::Positive,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn empty() -> Self {
|
|
||||||
Self::positive(sugg::ZERO)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn apply_offset(lhs: &MinifyingSugg<'static>, rhs: &Offset) -> MinifyingSugg<'static> {
|
|
||||||
match rhs.sign {
|
|
||||||
OffsetSign::Positive => lhs + &rhs.value,
|
|
||||||
OffsetSign::Negative => lhs - &rhs.value,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct MinifyingSugg<'a>(Sugg<'a>);
|
struct MinifyingSugg<'a>(Sugg<'a>);
|
||||||
|
|
||||||
|
@ -909,6 +875,46 @@ impl std::ops::Sub<&MinifyingSugg<'static>> for MinifyingSugg<'static> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// a wrapper around `MinifyingSugg`, which carries a operator like currying
|
||||||
|
/// so that the suggested code become more efficient (e.g. `foo + -bar` `foo - bar`).
|
||||||
|
struct Offset {
|
||||||
|
value: MinifyingSugg<'static>,
|
||||||
|
sign: OffsetSign,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
enum OffsetSign {
|
||||||
|
Positive,
|
||||||
|
Negative,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Offset {
|
||||||
|
fn negative(value: Sugg<'static>) -> Self {
|
||||||
|
Self {
|
||||||
|
value: value.into(),
|
||||||
|
sign: OffsetSign::Negative,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn positive(value: Sugg<'static>) -> Self {
|
||||||
|
Self {
|
||||||
|
value: value.into(),
|
||||||
|
sign: OffsetSign::Positive,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn empty() -> Self {
|
||||||
|
Self::positive(sugg::ZERO)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_offset(lhs: &MinifyingSugg<'static>, rhs: &Offset) -> MinifyingSugg<'static> {
|
||||||
|
match rhs.sign {
|
||||||
|
OffsetSign::Positive => lhs + &rhs.value,
|
||||||
|
OffsetSign::Negative => lhs - &rhs.value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
enum StartKind<'hir> {
|
enum StartKind<'hir> {
|
||||||
Range,
|
Range,
|
||||||
|
|
Loading…
Reference in a new issue