Skip to content

Commit 76d66e6

Browse files
author
Michael Wright
committed
Merge branch 'master' into dev-fmt-4
2 parents 2c90083 + b029042 commit 76d66e6

12 files changed

+54
-34
lines changed

appveyor.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ environment:
88
- TARGET: x86_64-pc-windows-msvc
99

1010
branches:
11-
# Only build AppVeyor on r+, try and the master branch
11+
# Only build AppVeyor on r+ and try branch
1212
only:
1313
- auto
1414
- try
15-
- master
1615

1716
install:
1817
- curl -sSf -o rustup-init.exe https://win.rustup.rs/

clippy_lints/src/misc.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc::ty;
77
use rustc::{declare_lint_pass, declare_tool_lint};
88
use rustc_errors::Applicability;
99
use syntax::ast::LitKind;
10-
use syntax::source_map::{ExpnFormat, Span};
10+
use syntax::source_map::{ExpnKind, Span};
1111

1212
use crate::consts::{constant, Constant};
1313
use crate::utils::sugg::Sugg;
@@ -596,10 +596,14 @@ fn is_used(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
596596
/// Tests whether an expression is in a macro expansion (e.g., something
597597
/// generated by `#[derive(...)]` or the like).
598598
fn in_attributes_expansion(expr: &Expr) -> bool {
599-
expr.span
600-
.ctxt()
601-
.outer_expn_info()
602-
.map_or(false, |info| matches!(info.format, ExpnFormat::MacroAttribute(_)))
599+
use syntax::ext::hygiene::MacroKind;
600+
expr.span.ctxt().outer_expn_info().map_or(false, |info| {
601+
if let ExpnKind::Macro(MacroKind::Attr, _) = info.kind {
602+
true
603+
} else {
604+
false
605+
}
606+
})
603607
}
604608

605609
/// Tests whether `res` is a variable defined outside a macro.

clippy_lints/src/returns.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ declare_clippy_lint! {
8686
#[derive(PartialEq, Eq, Copy, Clone)]
8787
enum RetReplacement {
8888
Empty,
89-
Unit,
89+
Block,
9090
}
9191

9292
declare_lint_pass!(Return => [NEEDLESS_RETURN, LET_AND_RETURN, UNUSED_UNIT]);
@@ -139,7 +139,7 @@ impl Return {
139139
// a match expr, check all arms
140140
ast::ExprKind::Match(_, ref arms) => {
141141
for arm in arms {
142-
self.check_final_expr(cx, &arm.body, Some(arm.body.span), RetReplacement::Unit);
142+
self.check_final_expr(cx, &arm.body, Some(arm.body.span), RetReplacement::Block);
143143
}
144144
},
145145
_ => (),
@@ -176,12 +176,12 @@ impl Return {
176176
);
177177
});
178178
},
179-
RetReplacement::Unit => {
179+
RetReplacement::Block => {
180180
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded return statement", |db| {
181181
db.span_suggestion(
182182
ret_span,
183-
"replace `return` with the unit type",
184-
"()".to_string(),
183+
"replace `return` with an empty block",
184+
"{}".to_string(),
185185
Applicability::MachineApplicable,
186186
);
187187
});
@@ -317,7 +317,7 @@ fn attr_is_cfg(attr: &ast::Attribute) -> bool {
317317

318318
// get the def site
319319
fn get_def(span: Span) -> Option<Span> {
320-
span.ctxt().outer_expn_info().and_then(|info| info.def_site)
320+
span.ctxt().outer_expn_info().and_then(|info| Some(info.def_site))
321321
}
322322

323323
// is this expr a `()` unit?

clippy_lints/src/strings.rs

+4
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ fn is_add(cx: &LateContext<'_, '_>, src: &Expr, target: &Expr) -> bool {
138138
}
139139
}
140140

141+
// Max length a b"foo" string can take
142+
const MAX_LENGTH_BYTE_STRING_LIT: usize = 32;
143+
141144
declare_lint_pass!(StringLitAsBytes => [STRING_LIT_AS_BYTES]);
142145

143146
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {
@@ -173,6 +176,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {
173176
);
174177
} else if callsite == expanded
175178
&& lit_content.as_str().chars().all(|c| c.is_ascii())
179+
&& lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT
176180
&& !in_macro_or_desugar(args[0].span)
177181
{
178182
span_lint_and_sugg(

clippy_lints/src/types.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
621621
}
622622

623623
fn is_questionmark_desugar_marked_call(expr: &Expr) -> bool {
624-
use syntax_pos::hygiene::CompilerDesugaringKind;
624+
use syntax_pos::hygiene::DesugaringKind;
625625
if let ExprKind::Call(ref callee, _) = expr.node {
626-
callee.span.is_compiler_desugaring(CompilerDesugaringKind::QuestionMark)
626+
callee.span.is_desugaring(DesugaringKind::QuestionMark)
627627
} else {
628628
false
629629
}
@@ -789,7 +789,8 @@ declare_clippy_lint! {
789789
/// **Why is this bad?** Dereferencing the resulting pointer may be undefined
790790
/// behavior.
791791
///
792-
/// **Known problems:** None.
792+
/// **Known problems:** Using `std::ptr::read_unaligned` and `std::ptr::write_unaligned` or similar
793+
/// on the resulting pointer is fine.
793794
///
794795
/// **Example:**
795796
/// ```rust
@@ -1210,17 +1211,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts {
12101211
if_chain! {
12111212
if let ty::RawPtr(from_ptr_ty) = &cast_from.sty;
12121213
if let ty::RawPtr(to_ptr_ty) = &cast_to.sty;
1213-
if let Some(from_align) = cx.layout_of(from_ptr_ty.ty).ok().map(|a| a.align.abi);
1214-
if let Some(to_align) = cx.layout_of(to_ptr_ty.ty).ok().map(|a| a.align.abi);
1215-
if from_align < to_align;
1214+
if let Ok(from_layout) = cx.layout_of(from_ptr_ty.ty);
1215+
if let Ok(to_layout) = cx.layout_of(to_ptr_ty.ty);
1216+
if from_layout.align.abi < to_layout.align.abi;
12161217
// with c_void, we inherently need to trust the user
12171218
if !is_c_void(cx, from_ptr_ty.ty);
1219+
// when casting from a ZST, we don't know enough to properly lint
1220+
if !from_layout.is_zst();
12181221
then {
12191222
span_lint(
12201223
cx,
12211224
CAST_PTR_ALIGNMENT,
12221225
expr.span,
1223-
&format!("casting from `{}` to a more-strictly-aligned pointer (`{}`)", cast_from, cast_to)
1226+
&format!(
1227+
"casting from `{}` to a more-strictly-aligned pointer (`{}`) ({} < {} bytes)",
1228+
cast_from,
1229+
cast_to,
1230+
from_layout.align.abi.bytes(),
1231+
to_layout.align.abi.bytes(),
1232+
),
12241233
);
12251234
}
12261235
}

clippy_lints/src/utils/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use rustc_errors::Applicability;
4343
use smallvec::SmallVec;
4444
use syntax::ast::{self, LitKind};
4545
use syntax::attr;
46-
use syntax::ext::hygiene::ExpnFormat;
46+
use syntax::ext::hygiene::ExpnKind;
4747
use syntax::source_map::{Span, DUMMY_SP};
4848
use syntax::symbol::{kw, Symbol};
4949

@@ -100,7 +100,7 @@ pub fn in_macro_or_desugar(span: Span) -> bool {
100100
/// Returns `true` if this `expn_info` was expanded by any macro.
101101
pub fn in_macro(span: Span) -> bool {
102102
if let Some(info) = span.ctxt().outer_expn_info() {
103-
if let ExpnFormat::CompilerDesugaring(..) = info.format {
103+
if let ExpnKind::Desugaring(..) = info.kind {
104104
false
105105
} else {
106106
true
@@ -686,7 +686,7 @@ pub fn is_adjusted(cx: &LateContext<'_, '_>, e: &Expr) -> bool {
686686
/// See also `is_direct_expn_of`.
687687
pub fn is_expn_of(mut span: Span, name: &str) -> Option<Span> {
688688
loop {
689-
let span_name_span = span.ctxt().outer_expn_info().map(|ei| (ei.format.name(), ei.call_site));
689+
let span_name_span = span.ctxt().outer_expn_info().map(|ei| (ei.kind.descr(), ei.call_site));
690690

691691
match span_name_span {
692692
Some((mac_name, new_span)) if mac_name.as_str() == name => return Some(new_span),
@@ -706,7 +706,7 @@ pub fn is_expn_of(mut span: Span, name: &str) -> Option<Span> {
706706
/// `bar!` by
707707
/// `is_direct_expn_of`.
708708
pub fn is_direct_expn_of(span: Span, name: &str) -> Option<Span> {
709-
let span_name_span = span.ctxt().outer_expn_info().map(|ei| (ei.format.name(), ei.call_site));
709+
let span_name_span = span.ctxt().outer_expn_info().map(|ei| (ei.kind.descr(), ei.call_site));
710710

711711
match span_name_span {
712712
Some((mac_name, new_span)) if mac_name.as_str() == name => Some(new_span),

tests/ui/cast_alignment.rs

+2
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ fn main() {
2222
// For c_void, we should trust the user. See #2677
2323
(&1u32 as *const u32 as *const std::os::raw::c_void) as *const u32;
2424
(&1u32 as *const u32 as *const libc::c_void) as *const u32;
25+
// For ZST, we should trust the user. See #4256
26+
(&1u32 as *const u32 as *const ()) as *const u32;
2527
}

tests/ui/cast_alignment.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`)
1+
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) (1 < 2 bytes)
22
--> $DIR/cast_alignment.rs:12:5
33
|
44
LL | (&1u8 as *const u8) as *const u16;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::cast-ptr-alignment` implied by `-D warnings`
88

9-
error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`)
9+
error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1 < 2 bytes)
1010
--> $DIR/cast_alignment.rs:13:5
1111
|
1212
LL | (&mut 1u8 as *mut u8) as *mut u16;

tests/ui/needless_return.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ error: unneeded return statement
7070
--> $DIR/needless_return.rs:64:14
7171
|
7272
LL | _ => return,
73-
| ^^^^^^ help: replace `return` with the unit type: `()`
73+
| ^^^^^^ help: replace `return` with an empty block: `{}`
7474

7575
error: aborting due to 12 previous errors
7676

tests/ui/string_lit_as_bytes.fixed

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
fn str_lit_as_bytes() {
77
let bs = b"hello there";
88

9-
let bs = br###"raw string with three ### in it and some " ""###;
9+
let bs = br###"raw string with 3# plus " ""###;
1010

11-
// no warning, because this cannot be written as a byte string literal:
11+
// no warning, because these cannot be written as byte string literals:
1212
let ubs = "☃".as_bytes();
13+
let ubs = "hello there! this is a very long string".as_bytes();
1314

1415
let strify = stringify!(foobar).as_bytes();
1516

tests/ui/string_lit_as_bytes.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
fn str_lit_as_bytes() {
77
let bs = "hello there".as_bytes();
88

9-
let bs = r###"raw string with three ### in it and some " ""###.as_bytes();
9+
let bs = r###"raw string with 3# plus " ""###.as_bytes();
1010

11-
// no warning, because this cannot be written as a byte string literal:
11+
// no warning, because these cannot be written as byte string literals:
1212
let ubs = "☃".as_bytes();
13+
let ubs = "hello there! this is a very long string".as_bytes();
1314

1415
let strify = stringify!(foobar).as_bytes();
1516

tests/ui/string_lit_as_bytes.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ LL | let bs = "hello there".as_bytes();
99
error: calling `as_bytes()` on a string literal
1010
--> $DIR/string_lit_as_bytes.rs:9:14
1111
|
12-
LL | let bs = r###"raw string with three ### in it and some " ""###.as_bytes();
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with three ### in it and some " ""###`
12+
LL | let bs = r###"raw string with 3# plus " ""###.as_bytes();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with 3# plus " ""###`
1414

1515
error: calling `as_bytes()` on `include_str!(..)`
16-
--> $DIR/string_lit_as_bytes.rs:16:22
16+
--> $DIR/string_lit_as_bytes.rs:17:22
1717
|
1818
LL | let includestr = include_str!("entry.rs").as_bytes();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `include_bytes!(..)` instead: `include_bytes!("entry.rs")`

0 commit comments

Comments
 (0)