Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Oct 25, 2023
1 parent 0f04fcb commit f2a4a46
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 52 deletions.
4 changes: 3 additions & 1 deletion examples/experimental/derive_opportunity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ fn is_derived(cx: &LateContext<'_>, def_id: DefId) -> Option<Macro> {
// smoelius: I'm not sure whether `SyntaxExtension::builtin_name` would be the right
// thing to use here; regardless, I can't figure out how to retrieve that data:
// https://github.com/rust-lang/rust/blob/d651fa78cefecefa87fa3d7dc1e1389d275afb63/compiler/rustc_expand/src/base.rs#L729-L731
Some(Macro::Builtin(*cx.get_def_path(macro_def_id).last().unwrap()))
Some(Macro::Builtin(
*cx.get_def_path(macro_def_id).last().unwrap(),
))
} else {
Some(Macro::External(macro_def_id))
}
Expand Down
22 changes: 12 additions & 10 deletions examples/general/incorrect_matches_operation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ dylint_linting::declare_pre_expansion_lint! {

fn is_matches_macro(expr: &P<Expr>) -> Option<&P<MacCall>> {
if_chain! {
if let ExprKind::MacCall(mac) = &expr.kind; // must be a macro call
if mac.path == sym!(matches); // must be a matches! symbol
if let ExprKind::MacCall(mac) = &expr.kind; // must be a macro call
if mac.path == sym!(matches); // must be a matches! symbol
then {
return Some(mac);
}
Expand Down Expand Up @@ -111,7 +111,10 @@ impl EarlyLintPass for IncorrectMatchesOperation {
// Look for binary operators
if let ExprKind::Binary(op, left, right) = &expr.kind;
// Ensure the binary operator is |, ||, &&, &
if matches!(op.node, BinOpKind::BitOr | BinOpKind::Or | BinOpKind::And | BinOpKind::BitAnd);
if matches!(
op.node,
BinOpKind::BitOr | BinOpKind::Or | BinOpKind::And | BinOpKind::BitAnd
);
// The left side needs to be a matches! macro call
if let Some(matches1) = is_matches_macro(left);
// The right side needs to be a matches! macro call
Expand All @@ -133,14 +136,13 @@ impl EarlyLintPass for IncorrectMatchesOperation {
}
// For & and && operators, this is likely a bug
BinOpKind::BitAnd | BinOpKind::And => {
let op = if op.node == BinOpKind::BitAnd { "&" } else { "&&" };
let op = if op.node == BinOpKind::BitAnd {
"&"
} else {
"&&"
};
let msg = format!("Is this a bug? matches!(obj, A) {op} matches!(obj, B) is (almost) always false");
span_lint(
cx,
INCORRECT_MATCHES_OPERATION,
expr.span,
&msg,
);
span_lint(cx, INCORRECT_MATCHES_OPERATION, expr.span, &msg);
}
_ => {
unreachable!("This should never happen - op.node can't be other operator");
Expand Down
1 change: 0 additions & 1 deletion examples/general/incorrect_matches_operation/ui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ fn main() {
// This one will not error out
let _c = matches!(x, 2) | matches!(_b, true);
}

69 changes: 35 additions & 34 deletions examples/restriction/collapsible_unwrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,39 +83,38 @@ impl CollapsibleUnwrap {
if let Some((method, mut recv, _, _, span)) = method_call(expr);
let snip_span = trim_span(cx.sess().source_map(), span.with_lo(recv.span.hi()));
if let Some(snip) = snippet_opt(cx, snip_span);
if let Some((span_sugg, prefix)) =
if method == "and_then" {
Some((&mut and_then_span_sugg, snip))
} else if let Some((inner_method, inner_recv, _, _, _)) = method_call(recv)
&& inner_method == "unwrap"
{
if and_then_span_sugg.is_none() {
and_then_span_sugg = Some(SpanSugg {
span: expr.span.with_lo(expr.span.hi()),
sugg: String::new(),
});
}
unwrap_span_sugg = and_then_span_sugg.clone();
let needs_mut = cx
.typeck_results()
.type_dependent_def_id(expr.hir_id)
.map_or(false, |def_id| has_ref_mut_self(cx, def_id));
let recv_ty = cx.typeck_results().expr_ty(recv);
let name = suggest_name_from_type(cx, recv_ty);
recv = inner_recv;
Some((
&mut unwrap_span_sugg,
format!(
".and_then(|{}{}| {}{})",
if needs_mut { "mut "} else { "" },
name,
name,
snip
)
))
} else {
None
};
if let Some((span_sugg, prefix)) = if method == "and_then" {
Some((&mut and_then_span_sugg, snip))
} else if let Some((inner_method, inner_recv, _, _, _)) = method_call(recv)
&& inner_method == "unwrap"
{
if and_then_span_sugg.is_none() {
and_then_span_sugg = Some(SpanSugg {
span: expr.span.with_lo(expr.span.hi()),
sugg: String::new(),
});
}
unwrap_span_sugg = and_then_span_sugg.clone();
let needs_mut = cx
.typeck_results()
.type_dependent_def_id(expr.hir_id)
.map_or(false, |def_id| has_ref_mut_self(cx, def_id));
let recv_ty = cx.typeck_results().expr_ty(recv);
let name = suggest_name_from_type(cx, recv_ty);
recv = inner_recv;
Some((
&mut unwrap_span_sugg,
format!(
".and_then(|{}{}| {}{})",
if needs_mut { "mut " } else { "" },
name,
name,
snip
),
))
} else {
None
};
let expr_ty = cx.typeck_results().expr_ty(expr);
let expr_err_ty = result_err_ty(cx, expr_ty);
let recv_ty = cx.typeck_results().expr_ty(recv);
Expand Down Expand Up @@ -154,7 +153,9 @@ impl CollapsibleUnwrap {
}
}

if let Some(SpanSugg { span, sugg }) = unwrap_span_sugg && !span.is_empty() {
if let Some(SpanSugg { span, sugg }) = unwrap_span_sugg
&& !span.is_empty()
{
span_lint_and_sugg(
cx,
COLLAPSIBLE_UNWRAP,
Expand Down
4 changes: 3 additions & 1 deletion examples/restriction/overscoped_allow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ fn read_diagnostics() -> Result<Vec<Diagnostic>> {
let mut diagnostics = Vec::new();
for result in serde_json::Deserializer::from_reader(file).into_iter::<Message>() {
let message = result?;
if message.reason == "compiler-message" && let Some(diagnostic) = message.message {
if message.reason == "compiler-message"
&& let Some(diagnostic) = message.message
{
diagnostics.push(diagnostic);
}
}
Expand Down
20 changes: 15 additions & 5 deletions examples/supplementary/unnecessary_conversion_for_trait/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryConversionForTrait {
(false, "inner argument")
};
let msg = format!("the {subject} implements the required traits");
if is_bare_method_call && refs_prefix.is_empty() && !maybe_arg.span.from_expansion() {
if is_bare_method_call
&& refs_prefix.is_empty()
&& !maybe_arg.span.from_expansion()
{
span_lint_and_sugg(
cx,
UNNECESSARY_CONVERSION_FOR_TRAIT,
Expand All @@ -260,7 +263,9 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryConversionForTrait {
String::new(),
Applicability::MachineApplicable,
);
} else if maybe_arg.span.from_expansion() && let Some(span) = maybe_arg.span.parent_callsite() {
} else if maybe_arg.span.from_expansion()
&& let Some(span) = maybe_arg.span.parent_callsite()
{
// smoelius: This message could be more informative.
span_lint_and_help(
cx,
Expand Down Expand Up @@ -610,11 +615,16 @@ fn replace_types<'tcx>(
{
let projection = cx.tcx.mk_ty_from_kind(ty::Alias(
ty::Projection,
projection_predicate.projection_ty.with_self_ty(cx.tcx, new_ty),
projection_predicate
.projection_ty
.with_self_ty(cx.tcx, new_ty),
));

if let Ok(projected_ty) = cx.tcx.try_normalize_erasing_regions(cx.param_env, projection)
&& substs[term_param_ty.index as usize] != ty::GenericArg::from(projected_ty)
if let Ok(projected_ty) = cx
.tcx
.try_normalize_erasing_regions(cx.param_env, projection)
&& substs[term_param_ty.index as usize]
!= ty::GenericArg::from(projected_ty)
{
deque.push_back((*term_param_ty, projected_ty));
}
Expand Down

0 comments on commit f2a4a46

Please sign in to comment.