mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
Added "make_return" and "blockify" convenience methods in Sugg and used them in "needless_bool".
This commit is contained in:
parent
9d5b148648
commit
0f5c43a722
@ -70,16 +70,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
|
|||||||
let reduce = |ret, not| {
|
let reduce = |ret, not| {
|
||||||
let mut applicability = Applicability::MachineApplicable;
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
let snip = Sugg::hir_with_applicability(cx, pred, "<predicate>", &mut applicability);
|
let snip = Sugg::hir_with_applicability(cx, pred, "<predicate>", &mut applicability);
|
||||||
let snip = if not { !snip } else { snip };
|
let mut snip = if not { !snip } else { snip };
|
||||||
|
|
||||||
let mut hint = if ret {
|
if ret {
|
||||||
format!("return {}", snip)
|
snip = snip.make_return();
|
||||||
} else {
|
}
|
||||||
snip.to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
if parent_node_is_if_expr(&e, &cx) {
|
if parent_node_is_if_expr(&e, &cx) {
|
||||||
hint = format!("{{ {} }}", hint);
|
snip = snip.blockify()
|
||||||
}
|
}
|
||||||
|
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
@ -88,7 +86,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
|
|||||||
e.span,
|
e.span,
|
||||||
"this if-then-else expression returns a bool literal",
|
"this if-then-else expression returns a bool literal",
|
||||||
"you can reduce it to",
|
"you can reduce it to",
|
||||||
hint,
|
snip.to_string(),
|
||||||
applicability,
|
applicability,
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -206,6 +206,17 @@ impl<'a> Sugg<'a> {
|
|||||||
make_unop("&mut *", self)
|
make_unop("&mut *", self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convenience method to transform suggestion into a return call
|
||||||
|
pub fn make_return(self) -> Sugg<'static> {
|
||||||
|
Sugg::NonParen(Cow::Owned(format!("return {}", self)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convenience method to transform suggestion into a block
|
||||||
|
/// where the suggestion is a trailing expression
|
||||||
|
pub fn blockify(self) -> Sugg<'static> {
|
||||||
|
Sugg::NonParen(Cow::Owned(format!("{{ {} }}", self)))
|
||||||
|
}
|
||||||
|
|
||||||
/// Convenience method to create the `<lhs>..<rhs>` or `<lhs>...<rhs>`
|
/// Convenience method to create the `<lhs>..<rhs>` or `<lhs>...<rhs>`
|
||||||
/// suggestion.
|
/// suggestion.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@ -578,3 +589,21 @@ impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_error
|
|||||||
self.span_suggestion_with_applicability(remove_span, msg, String::new(), applicability);
|
self.span_suggestion_with_applicability(remove_span, msg, String::new(), applicability);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::Sugg;
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
const SUGGESTION: Sugg<'static> = Sugg::NonParen(Cow::Borrowed("function_call()"));
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn make_return_transform_sugg_into_a_return_call() {
|
||||||
|
assert_eq!("return function_call()", SUGGESTION.make_return().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn blockify_transforms_sugg_into_a_block() {
|
||||||
|
assert_eq!("{ function_call() }", SUGGESTION.blockify().to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user