Merge branch 'master' into fix_cow

This commit is contained in:
Oliver Schneider 2018-03-05 08:42:19 +01:00 committed by GitHub
commit 6662aa41f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 88 additions and 58 deletions

View file

@ -43,7 +43,7 @@ cargo_metadata = "0.2"
regex = "0.2" regex = "0.2"
[dev-dependencies] [dev-dependencies]
compiletest_rs = "0.3.6" compiletest_rs = "0.3.7"
duct = "0.8.2" duct = "0.8.2"
lazy_static = "1.0" lazy_static = "1.0"
serde_derive = "1.0" serde_derive = "1.0"

View file

@ -134,7 +134,7 @@ impl<'a> DigitInfo<'a> {
let mut last_d = '\0'; let mut last_d = '\0';
for (d_idx, d) in sans_prefix.char_indices() { for (d_idx, d) in sans_prefix.char_indices() {
if !float && (d == 'i' || d == 'u') || float && d == 'f' { if !float && (d == 'i' || d == 'u') || float && (d == 'f' || d == 'e' || d == 'E') {
let suffix_start = if last_d == '_' { d_idx - 1 } else { d_idx }; let suffix_start = if last_d == '_' { d_idx - 1 } else { d_idx };
let (digits, suffix) = sans_prefix.split_at(suffix_start); let (digits, suffix) = sans_prefix.split_at(suffix_start);
return Self { return Self {
@ -285,60 +285,64 @@ impl EarlyLintPass for LiteralDigitGrouping {
impl LiteralDigitGrouping { impl LiteralDigitGrouping {
fn check_lit(&self, cx: &EarlyContext, lit: &Lit) { fn check_lit(&self, cx: &EarlyContext, lit: &Lit) {
// Lint integral literals. match lit.node {
if_chain! { LitKind::Int(..) => {
if let LitKind::Int(..) = lit.node; // Lint integral literals.
if let Some(src) = snippet_opt(cx, lit.span); if_chain! {
if let Some(firstch) = src.chars().next(); if let Some(src) = snippet_opt(cx, lit.span);
if char::to_digit(firstch, 10).is_some(); if let Some(firstch) = src.chars().next();
then { if char::to_digit(firstch, 10).is_some();
let digit_info = DigitInfo::new(&src, false); then {
let _ = Self::do_lint(digit_info.digits).map_err(|warning_type| { let digit_info = DigitInfo::new(&src, false);
warning_type.display(&digit_info.grouping_hint(), cx, &lit.span) let _ = Self::do_lint(digit_info.digits).map_err(|warning_type| {
}); warning_type.display(&digit_info.grouping_hint(), cx, &lit.span)
} });
} }
}
},
LitKind::Float(..) | LitKind::FloatUnsuffixed(..) => {
// Lint floating-point literals.
if_chain! {
if let Some(src) = snippet_opt(cx, lit.span);
if let Some(firstch) = src.chars().next();
if char::to_digit(firstch, 10).is_some();
then {
let digit_info = DigitInfo::new(&src, true);
// Separate digits into integral and fractional parts.
let parts: Vec<&str> = digit_info
.digits
.split_terminator('.')
.collect();
// Lint floating-point literals. // Lint integral and fractional parts separately, and then check consistency of digit
if_chain! { // groups if both pass.
if let LitKind::Float(..) = lit.node; let _ = Self::do_lint(parts[0])
if let Some(src) = snippet_opt(cx, lit.span); .map(|integral_group_size| {
if let Some(firstch) = src.chars().next(); if parts.len() > 1 {
if char::to_digit(firstch, 10).is_some(); // Lint the fractional part of literal just like integral part, but reversed.
then { let fractional_part = &parts[1].chars().rev().collect::<String>();
let digit_info = DigitInfo::new(&src, true); let _ = Self::do_lint(fractional_part)
// Separate digits into integral and fractional parts. .map(|fractional_group_size| {
let parts: Vec<&str> = digit_info let consistent = Self::parts_consistent(integral_group_size,
.digits fractional_group_size,
.split_terminator('.') parts[0].len(),
.collect(); parts[1].len());
if !consistent {
// Lint integral and fractional parts separately, and then check consistency of digit WarningType::InconsistentDigitGrouping.display(&digit_info.grouping_hint(),
// groups if both pass. cx,
let _ = Self::do_lint(parts[0]) &lit.span);
.map(|integral_group_size| { }
if parts.len() > 1 { })
// Lint the fractional part of literal just like integral part, but reversed. .map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(),
let fractional_part = &parts[1].chars().rev().collect::<String>(); cx,
let _ = Self::do_lint(fractional_part) &lit.span));
.map(|fractional_group_size| { }
let consistent = Self::parts_consistent(integral_group_size, })
fractional_group_size, .map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(), cx, &lit.span));
parts[0].len(), }
parts[1].len()); }
if !consistent { },
WarningType::InconsistentDigitGrouping.display(&digit_info.grouping_hint(), _ => (),
cx,
&lit.span);
}
})
.map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(),
cx,
&lit.span));
}
})
.map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(), cx, &lit.span));
}
} }
} }

View file

@ -1624,7 +1624,8 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
if_chain! { if_chain! {
// a range index op // a range index op
if let ExprMethodCall(ref meth, _, ref args) = expr.node; if let ExprMethodCall(ref meth, _, ref args) = expr.node;
if meth.name == "index" || meth.name == "index_mut"; if (meth.name == "index" && match_trait_method(self.cx, expr, &paths::INDEX))
|| (meth.name == "index_mut" && match_trait_method(self.cx, expr, &paths::INDEX_MUT));
if !self.check(&args[1], &args[0], expr); if !self.check(&args[1], &args[0], expr);
then { return } then { return }
} }

View file

@ -33,6 +33,8 @@ pub const HASH: [&str; 2] = ["hash", "Hash"];
pub const HASHMAP: [&str; 5] = ["std", "collections", "hash", "map", "HashMap"]; pub const HASHMAP: [&str; 5] = ["std", "collections", "hash", "map", "HashMap"];
pub const HASHMAP_ENTRY: [&str; 5] = ["std", "collections", "hash", "map", "Entry"]; pub const HASHMAP_ENTRY: [&str; 5] = ["std", "collections", "hash", "map", "Entry"];
pub const HASHSET: [&str; 5] = ["std", "collections", "hash", "set", "HashSet"]; pub const HASHSET: [&str; 5] = ["std", "collections", "hash", "set", "HashSet"];
pub const INDEX: [&str; 3] = ["core", "ops", "Index"];
pub const INDEX_MUT: [&str; 3] = ["core", "ops", "IndexMut"];
pub const INIT: [&str; 4] = ["core", "intrinsics", "", "init"]; pub const INIT: [&str; 4] = ["core", "intrinsics", "", "init"];
pub const INTO: [&str; 3] = ["core", "convert", "Into"]; pub const INTO: [&str; 3] = ["core", "convert", "Into"];
pub const INTO_ITERATOR: [&str; 4] = ["core", "iter", "traits", "IntoIterator"]; pub const INTO_ITERATOR: [&str; 4] = ["core", "iter", "traits", "IntoIterator"];

View file

@ -2,7 +2,7 @@
#[warn(approx_constant)] #[warn(approx_constant)]
#[allow(unused, shadow_unrelated, similar_names)] #[allow(unused, shadow_unrelated, similar_names, unreadable_literal)]
fn main() { fn main() {
let my_e = 2.7182; let my_e = 2.7182;
let almost_e = 2.718; let almost_e = 2.718;

View file

@ -556,3 +556,18 @@ pub fn manual_copy_same_destination(dst: &mut [i32], d: usize, s: usize) {
dst[d + i] = dst[s + i]; dst[d + i] = dst[s + i];
} }
} }
mod issue_2496 {
pub trait Handle {
fn new_for_index(index: usize) -> Self;
fn index(&self) -> usize;
}
pub fn test<H: Handle>() -> H {
for x in 0..5 {
let next_handle = H::new_for_index(x);
println!("{}", next_handle.index());
}
unimplemented!()
}
}

View file

@ -5,4 +5,6 @@
fn main() { fn main() {
let good = (0b1011_i64, 0o1_234_u32, 0x1_234_567, 1_2345_6789, 1234_f32, 1_234.12_f32, 1_234.123_f32, 1.123_4_f32); let good = (0b1011_i64, 0o1_234_u32, 0x1_234_567, 1_2345_6789, 1234_f32, 1_234.12_f32, 1_234.123_f32, 1.123_4_f32);
let bad = (0b10110_i64, 0x12345678901_usize, 12345_f32, 1.23456_f32); let bad = (0b10110_i64, 0x12345678901_usize, 12345_f32, 1.23456_f32);
let good_sci = 1.1234e1;
let bad_sci = 1.12345e1;
} }

View file

@ -24,5 +24,11 @@ error: long literal lacking separators
7 | let bad = (0b10110_i64, 0x12345678901_usize, 12345_f32, 1.23456_f32); 7 | let bad = (0b10110_i64, 0x12345678901_usize, 12345_f32, 1.23456_f32);
| ^^^^^^^^^^^ help: consider: `1.234_56_f32` | ^^^^^^^^^^^ help: consider: `1.234_56_f32`
error: aborting due to 4 previous errors error: long literal lacking separators
--> $DIR/unreadable_literal.rs:9:19
|
9 | let bad_sci = 1.12345e1;
| ^^^^^^^^^ help: consider: `1.123_45e1`
error: aborting due to 5 previous errors