Skip to content

Commit 232dd43

Browse files
author
Michael Wright
committed
Fix occurrences of too_many_lines violations
1 parent 0ae1a69 commit 232dd43

File tree

4 files changed

+164
-140
lines changed

4 files changed

+164
-140
lines changed

clippy_lints/src/ranges.rs

Lines changed: 73 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -151,75 +151,82 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Ranges {
151151
}
152152
}
153153

154-
// exclusive range plus one: `x..(y+1)`
155-
if_chain! {
156-
if let Some(higher::Range {
157-
start,
158-
end: Some(end),
159-
limits: RangeLimits::HalfOpen
160-
}) = higher::range(cx, expr);
161-
if let Some(y) = y_plus_one(end);
162-
then {
163-
let span = if expr.span.from_expansion() {
164-
expr.span
165-
.ctxt()
166-
.outer_expn_data()
167-
.call_site
168-
} else {
169-
expr.span
170-
};
171-
span_lint_and_then(
172-
cx,
173-
RANGE_PLUS_ONE,
174-
span,
175-
"an inclusive range would be more readable",
176-
|db| {
177-
let start = start.map_or(String::new(), |x| Sugg::hir(cx, x, "x").to_string());
178-
let end = Sugg::hir(cx, y, "y");
179-
if let Some(is_wrapped) = &snippet_opt(cx, span) {
180-
if is_wrapped.starts_with('(') && is_wrapped.ends_with(')') {
181-
db.span_suggestion(
182-
span,
183-
"use",
184-
format!("({}..={})", start, end),
185-
Applicability::MaybeIncorrect,
186-
);
187-
} else {
188-
db.span_suggestion(
189-
span,
190-
"use",
191-
format!("{}..={}", start, end),
192-
Applicability::MachineApplicable, // snippet
193-
);
194-
}
154+
check_exclusive_range_plus_one(cx, expr);
155+
check_inclusive_range_minus_one(cx, expr);
156+
}
157+
}
158+
159+
// exclusive range plus one: `x..(y+1)`
160+
fn check_exclusive_range_plus_one(cx: &LateContext<'_, '_>, expr: &Expr) {
161+
if_chain! {
162+
if let Some(higher::Range {
163+
start,
164+
end: Some(end),
165+
limits: RangeLimits::HalfOpen
166+
}) = higher::range(cx, expr);
167+
if let Some(y) = y_plus_one(end);
168+
then {
169+
let span = if expr.span.from_expansion() {
170+
expr.span
171+
.ctxt()
172+
.outer_expn_data()
173+
.call_site
174+
} else {
175+
expr.span
176+
};
177+
span_lint_and_then(
178+
cx,
179+
RANGE_PLUS_ONE,
180+
span,
181+
"an inclusive range would be more readable",
182+
|db| {
183+
let start = start.map_or(String::new(), |x| Sugg::hir(cx, x, "x").to_string());
184+
let end = Sugg::hir(cx, y, "y");
185+
if let Some(is_wrapped) = &snippet_opt(cx, span) {
186+
if is_wrapped.starts_with('(') && is_wrapped.ends_with(')') {
187+
db.span_suggestion(
188+
span,
189+
"use",
190+
format!("({}..={})", start, end),
191+
Applicability::MaybeIncorrect,
192+
);
193+
} else {
194+
db.span_suggestion(
195+
span,
196+
"use",
197+
format!("{}..={}", start, end),
198+
Applicability::MachineApplicable, // snippet
199+
);
195200
}
196-
},
197-
);
198-
}
201+
}
202+
},
203+
);
199204
}
205+
}
206+
}
200207

201-
// inclusive range minus one: `x..=(y-1)`
202-
if_chain! {
203-
if let Some(higher::Range { start, end: Some(end), limits: RangeLimits::Closed }) = higher::range(cx, expr);
204-
if let Some(y) = y_minus_one(end);
205-
then {
206-
span_lint_and_then(
207-
cx,
208-
RANGE_MINUS_ONE,
209-
expr.span,
210-
"an exclusive range would be more readable",
211-
|db| {
212-
let start = start.map_or(String::new(), |x| Sugg::hir(cx, x, "x").to_string());
213-
let end = Sugg::hir(cx, y, "y");
214-
db.span_suggestion(
215-
expr.span,
216-
"use",
217-
format!("{}..{}", start, end),
218-
Applicability::MachineApplicable, // snippet
219-
);
220-
},
221-
);
222-
}
208+
// inclusive range minus one: `x..=(y-1)`
209+
fn check_inclusive_range_minus_one(cx: &LateContext<'_, '_>, expr: &Expr) {
210+
if_chain! {
211+
if let Some(higher::Range { start, end: Some(end), limits: RangeLimits::Closed }) = higher::range(cx, expr);
212+
if let Some(y) = y_minus_one(end);
213+
then {
214+
span_lint_and_then(
215+
cx,
216+
RANGE_MINUS_ONE,
217+
expr.span,
218+
"an exclusive range would be more readable",
219+
|db| {
220+
let start = start.map_or(String::new(), |x| Sugg::hir(cx, x, "x").to_string());
221+
let end = Sugg::hir(cx, y, "y");
222+
db.span_suggestion(
223+
expr.span,
224+
"use",
225+
format!("{}..{}", start, end),
226+
Applicability::MachineApplicable, // snippet
227+
);
228+
},
229+
);
223230
}
224231
}
225232
}

clippy_lints/src/types.rs

Lines changed: 88 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,83 +1159,97 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts {
11591159
}
11601160
}
11611161
if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx.sess(), expr.span) {
1162-
match (cast_from.is_integral(), cast_to.is_integral()) {
1163-
(true, false) => {
1164-
let from_nbits = int_ty_to_nbits(cast_from, cx.tcx);
1165-
let to_nbits = if let ty::Float(FloatTy::F32) = cast_to.sty {
1166-
32
1167-
} else {
1168-
64
1169-
};
1170-
if is_isize_or_usize(cast_from) || from_nbits >= to_nbits {
1171-
span_precision_loss_lint(cx, expr, cast_from, to_nbits == 64);
1172-
}
1173-
if from_nbits < to_nbits {
1174-
span_lossless_lint(cx, expr, ex, cast_from, cast_to);
1175-
}
1176-
},
1177-
(false, true) => {
1178-
span_lint(
1179-
cx,
1180-
CAST_POSSIBLE_TRUNCATION,
1181-
expr.span,
1182-
&format!("casting {} to {} may truncate the value", cast_from, cast_to),
1183-
);
1184-
if !cast_to.is_signed() {
1185-
span_lint(
1186-
cx,
1187-
CAST_SIGN_LOSS,
1188-
expr.span,
1189-
&format!("casting {} to {} may lose the sign of the value", cast_from, cast_to),
1190-
);
1191-
}
1192-
},
1193-
(true, true) => {
1194-
check_loss_of_sign(cx, expr, ex, cast_from, cast_to);
1195-
check_truncation_and_wrapping(cx, expr, cast_from, cast_to);
1196-
check_lossless(cx, expr, ex, cast_from, cast_to);
1197-
},
1198-
(false, false) => {
1199-
if let (&ty::Float(FloatTy::F64), &ty::Float(FloatTy::F32)) = (&cast_from.sty, &cast_to.sty) {
1200-
span_lint(
1201-
cx,
1202-
CAST_POSSIBLE_TRUNCATION,
1203-
expr.span,
1204-
"casting f64 to f32 may truncate the value",
1205-
);
1206-
}
1207-
if let (&ty::Float(FloatTy::F32), &ty::Float(FloatTy::F64)) = (&cast_from.sty, &cast_to.sty) {
1208-
span_lossless_lint(cx, expr, ex, cast_from, cast_to);
1209-
}
1210-
},
1211-
}
1162+
lint_numeric_casts(cx, expr, ex, cast_from, cast_to);
12121163
}
12131164

1214-
if_chain! {
1215-
if let ty::RawPtr(from_ptr_ty) = &cast_from.sty;
1216-
if let ty::RawPtr(to_ptr_ty) = &cast_to.sty;
1217-
if let Ok(from_layout) = cx.layout_of(from_ptr_ty.ty);
1218-
if let Ok(to_layout) = cx.layout_of(to_ptr_ty.ty);
1219-
if from_layout.align.abi < to_layout.align.abi;
1220-
// with c_void, we inherently need to trust the user
1221-
if !is_c_void(cx, from_ptr_ty.ty);
1222-
// when casting from a ZST, we don't know enough to properly lint
1223-
if !from_layout.is_zst();
1224-
then {
1225-
span_lint(
1226-
cx,
1227-
CAST_PTR_ALIGNMENT,
1228-
expr.span,
1229-
&format!(
1230-
"casting from `{}` to a more-strictly-aligned pointer (`{}`) ({} < {} bytes)",
1231-
cast_from,
1232-
cast_to,
1233-
from_layout.align.abi.bytes(),
1234-
to_layout.align.abi.bytes(),
1235-
),
1236-
);
1237-
}
1165+
lint_cast_ptr_alignment(cx, expr, cast_from, cast_to);
1166+
}
1167+
}
1168+
}
1169+
1170+
fn lint_numeric_casts<'tcx>(
1171+
cx: &LateContext<'_, 'tcx>,
1172+
expr: &Expr,
1173+
cast_expr: &Expr,
1174+
cast_from: Ty<'tcx>,
1175+
cast_to: Ty<'tcx>,
1176+
) {
1177+
match (cast_from.is_integral(), cast_to.is_integral()) {
1178+
(true, false) => {
1179+
let from_nbits = int_ty_to_nbits(cast_from, cx.tcx);
1180+
let to_nbits = if let ty::Float(FloatTy::F32) = cast_to.sty {
1181+
32
1182+
} else {
1183+
64
1184+
};
1185+
if is_isize_or_usize(cast_from) || from_nbits >= to_nbits {
1186+
span_precision_loss_lint(cx, expr, cast_from, to_nbits == 64);
12381187
}
1188+
if from_nbits < to_nbits {
1189+
span_lossless_lint(cx, expr, cast_expr, cast_from, cast_to);
1190+
}
1191+
},
1192+
(false, true) => {
1193+
span_lint(
1194+
cx,
1195+
CAST_POSSIBLE_TRUNCATION,
1196+
expr.span,
1197+
&format!("casting {} to {} may truncate the value", cast_from, cast_to),
1198+
);
1199+
if !cast_to.is_signed() {
1200+
span_lint(
1201+
cx,
1202+
CAST_SIGN_LOSS,
1203+
expr.span,
1204+
&format!("casting {} to {} may lose the sign of the value", cast_from, cast_to),
1205+
);
1206+
}
1207+
},
1208+
(true, true) => {
1209+
check_loss_of_sign(cx, expr, cast_expr, cast_from, cast_to);
1210+
check_truncation_and_wrapping(cx, expr, cast_from, cast_to);
1211+
check_lossless(cx, expr, cast_expr, cast_from, cast_to);
1212+
},
1213+
(false, false) => {
1214+
if let (&ty::Float(FloatTy::F64), &ty::Float(FloatTy::F32)) = (&cast_from.sty, &cast_to.sty) {
1215+
span_lint(
1216+
cx,
1217+
CAST_POSSIBLE_TRUNCATION,
1218+
expr.span,
1219+
"casting f64 to f32 may truncate the value",
1220+
);
1221+
}
1222+
if let (&ty::Float(FloatTy::F32), &ty::Float(FloatTy::F64)) = (&cast_from.sty, &cast_to.sty) {
1223+
span_lossless_lint(cx, expr, cast_expr, cast_from, cast_to);
1224+
}
1225+
},
1226+
}
1227+
}
1228+
1229+
fn lint_cast_ptr_alignment<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr, cast_from: Ty<'tcx>, cast_to: Ty<'tcx>) {
1230+
if_chain! {
1231+
if let ty::RawPtr(from_ptr_ty) = &cast_from.sty;
1232+
if let ty::RawPtr(to_ptr_ty) = &cast_to.sty;
1233+
if let Ok(from_layout) = cx.layout_of(from_ptr_ty.ty);
1234+
if let Ok(to_layout) = cx.layout_of(to_ptr_ty.ty);
1235+
if from_layout.align.abi < to_layout.align.abi;
1236+
// with c_void, we inherently need to trust the user
1237+
if !is_c_void(cx, from_ptr_ty.ty);
1238+
// when casting from a ZST, we don't know enough to properly lint
1239+
if !from_layout.is_zst();
1240+
then {
1241+
span_lint(
1242+
cx,
1243+
CAST_PTR_ALIGNMENT,
1244+
expr.span,
1245+
&format!(
1246+
"casting from `{}` to a more-strictly-aligned pointer (`{}`) ({} < {} bytes)",
1247+
cast_from,
1248+
cast_to,
1249+
from_layout.align.abi.bytes(),
1250+
to_layout.align.abi.bytes(),
1251+
),
1252+
);
12391253
}
12401254
}
12411255
}

clippy_lints/src/utils/inspector.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ fn has_attr(sess: &Session, attrs: &[Attribute]) -> bool {
144144
}
145145

146146
#[allow(clippy::similar_names)]
147+
#[allow(clippy::too_many_lines)]
147148
fn print_expr(cx: &LateContext<'_, '_>, expr: &hir::Expr, indent: usize) {
148149
let ind = " ".repeat(indent);
149150
println!("{}+", ind);
@@ -396,6 +397,7 @@ fn print_item(cx: &LateContext<'_, '_>, item: &hir::Item) {
396397
}
397398

398399
#[allow(clippy::similar_names)]
400+
#[allow(clippy::too_many_lines)]
399401
fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat, indent: usize) {
400402
let ind = " ".repeat(indent);
401403
println!("{}+", ind);

clippy_lints/src/write.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ impl FmtStr {
320320
/// ```rust,ignore
321321
/// (Some("string to write: {}"), Some(buf))
322322
/// ```
323+
#[allow(clippy::too_many_lines)]
323324
fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &TokenStream, is_write: bool) -> (Option<FmtStr>, Option<Expr>) {
324325
use fmt_macros::*;
325326
let tts = tts.clone();

0 commit comments

Comments
 (0)