Skip to content

Commit

Permalink
Merge branch 'master' into dev-fmt-4
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wright committed Jul 13, 2019
2 parents 2c90083 + b029042 commit 76d66e6
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 34 deletions.
3 changes: 1 addition & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ environment:
- TARGET: x86_64-pc-windows-msvc

branches:
# Only build AppVeyor on r+, try and the master branch
# Only build AppVeyor on r+ and try branch
only:
- auto
- try
- master

install:
- curl -sSf -o rustup-init.exe https://win.rustup.rs/
Expand Down
14 changes: 9 additions & 5 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc::ty;
use rustc::{declare_lint_pass, declare_tool_lint};
use rustc_errors::Applicability;
use syntax::ast::LitKind;
use syntax::source_map::{ExpnFormat, Span};
use syntax::source_map::{ExpnKind, Span};

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

/// Tests whether `res` is a variable defined outside a macro.
Expand Down
12 changes: 6 additions & 6 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ declare_clippy_lint! {
#[derive(PartialEq, Eq, Copy, Clone)]
enum RetReplacement {
Empty,
Unit,
Block,
}

declare_lint_pass!(Return => [NEEDLESS_RETURN, LET_AND_RETURN, UNUSED_UNIT]);
Expand Down Expand Up @@ -139,7 +139,7 @@ impl Return {
// a match expr, check all arms
ast::ExprKind::Match(_, ref arms) => {
for arm in arms {
self.check_final_expr(cx, &arm.body, Some(arm.body.span), RetReplacement::Unit);
self.check_final_expr(cx, &arm.body, Some(arm.body.span), RetReplacement::Block);
}
},
_ => (),
Expand Down Expand Up @@ -176,12 +176,12 @@ impl Return {
);
});
},
RetReplacement::Unit => {
RetReplacement::Block => {
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded return statement", |db| {
db.span_suggestion(
ret_span,
"replace `return` with the unit type",
"()".to_string(),
"replace `return` with an empty block",
"{}".to_string(),
Applicability::MachineApplicable,
);
});
Expand Down Expand Up @@ -317,7 +317,7 @@ fn attr_is_cfg(attr: &ast::Attribute) -> bool {

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

// is this expr a `()` unit?
Expand Down
4 changes: 4 additions & 0 deletions clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ fn is_add(cx: &LateContext<'_, '_>, src: &Expr, target: &Expr) -> bool {
}
}

// Max length a b"foo" string can take
const MAX_LENGTH_BYTE_STRING_LIT: usize = 32;

declare_lint_pass!(StringLitAsBytes => [STRING_LIT_AS_BYTES]);

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {
Expand Down Expand Up @@ -173,6 +176,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringLitAsBytes {
);
} else if callsite == expanded
&& lit_content.as_str().chars().all(|c| c.is_ascii())
&& lit_content.as_str().len() <= MAX_LENGTH_BYTE_STRING_LIT
&& !in_macro_or_desugar(args[0].span)
{
span_lint_and_sugg(
Expand Down
23 changes: 16 additions & 7 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
}

fn is_questionmark_desugar_marked_call(expr: &Expr) -> bool {
use syntax_pos::hygiene::CompilerDesugaringKind;
use syntax_pos::hygiene::DesugaringKind;
if let ExprKind::Call(ref callee, _) = expr.node {
callee.span.is_compiler_desugaring(CompilerDesugaringKind::QuestionMark)
callee.span.is_desugaring(DesugaringKind::QuestionMark)
} else {
false
}
Expand Down Expand Up @@ -789,7 +789,8 @@ declare_clippy_lint! {
/// **Why is this bad?** Dereferencing the resulting pointer may be undefined
/// behavior.
///
/// **Known problems:** None.
/// **Known problems:** Using `std::ptr::read_unaligned` and `std::ptr::write_unaligned` or similar
/// on the resulting pointer is fine.
///
/// **Example:**
/// ```rust
Expand Down Expand Up @@ -1210,17 +1211,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts {
if_chain! {
if let ty::RawPtr(from_ptr_ty) = &cast_from.sty;
if let ty::RawPtr(to_ptr_ty) = &cast_to.sty;
if let Some(from_align) = cx.layout_of(from_ptr_ty.ty).ok().map(|a| a.align.abi);
if let Some(to_align) = cx.layout_of(to_ptr_ty.ty).ok().map(|a| a.align.abi);
if from_align < to_align;
if let Ok(from_layout) = cx.layout_of(from_ptr_ty.ty);
if let Ok(to_layout) = cx.layout_of(to_ptr_ty.ty);
if from_layout.align.abi < to_layout.align.abi;
// with c_void, we inherently need to trust the user
if !is_c_void(cx, from_ptr_ty.ty);
// when casting from a ZST, we don't know enough to properly lint
if !from_layout.is_zst();
then {
span_lint(
cx,
CAST_PTR_ALIGNMENT,
expr.span,
&format!("casting from `{}` to a more-strictly-aligned pointer (`{}`)", cast_from, cast_to)
&format!(
"casting from `{}` to a more-strictly-aligned pointer (`{}`) ({} < {} bytes)",
cast_from,
cast_to,
from_layout.align.abi.bytes(),
to_layout.align.abi.bytes(),
),
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use rustc_errors::Applicability;
use smallvec::SmallVec;
use syntax::ast::{self, LitKind};
use syntax::attr;
use syntax::ext::hygiene::ExpnFormat;
use syntax::ext::hygiene::ExpnKind;
use syntax::source_map::{Span, DUMMY_SP};
use syntax::symbol::{kw, Symbol};

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

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

match span_name_span {
Some((mac_name, new_span)) if mac_name.as_str() == name => Some(new_span),
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/cast_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ fn main() {
// For c_void, we should trust the user. See #2677
(&1u32 as *const u32 as *const std::os::raw::c_void) as *const u32;
(&1u32 as *const u32 as *const libc::c_void) as *const u32;
// For ZST, we should trust the user. See #4256
(&1u32 as *const u32 as *const ()) as *const u32;
}
4 changes: 2 additions & 2 deletions tests/ui/cast_alignment.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`)
error: casting from `*const u8` to a more-strictly-aligned pointer (`*const u16`) (1 < 2 bytes)
--> $DIR/cast_alignment.rs:12:5
|
LL | (&1u8 as *const u8) as *const u16;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::cast-ptr-alignment` implied by `-D warnings`

error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`)
error: casting from `*mut u8` to a more-strictly-aligned pointer (`*mut u16`) (1 < 2 bytes)
--> $DIR/cast_alignment.rs:13:5
|
LL | (&mut 1u8 as *mut u8) as *mut u16;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/needless_return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ error: unneeded return statement
--> $DIR/needless_return.rs:64:14
|
LL | _ => return,
| ^^^^^^ help: replace `return` with the unit type: `()`
| ^^^^^^ help: replace `return` with an empty block: `{}`

error: aborting due to 12 previous errors

5 changes: 3 additions & 2 deletions tests/ui/string_lit_as_bytes.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
fn str_lit_as_bytes() {
let bs = b"hello there";

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

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

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

Expand Down
5 changes: 3 additions & 2 deletions tests/ui/string_lit_as_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
fn str_lit_as_bytes() {
let bs = "hello there".as_bytes();

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

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

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

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/string_lit_as_bytes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ LL | let bs = "hello there".as_bytes();
error: calling `as_bytes()` on a string literal
--> $DIR/string_lit_as_bytes.rs:9:14
|
LL | let bs = r###"raw string with three ### in it and some " ""###.as_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with three ### in it and some " ""###`
LL | let bs = r###"raw string with 3# plus " ""###.as_bytes();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using a byte string literal instead: `br###"raw string with 3# plus " ""###`

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

0 comments on commit 76d66e6

Please sign in to comment.