Skip to content

Commit 3899d60

Browse files
committed
Auto merge of rust-lang#5878 - flip1995:rustup, r=flip1995
Rustup r? @ghost changelog: none
2 parents 2d4c337 + fd87cdb commit 3899d60

9 files changed

+52
-81
lines changed

clippy_lints/src/doc.rs

+36-45
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::utils::{implements_trait, is_entrypoint_fn, is_type_diagnostic_item,
22
use if_chain::if_chain;
33
use itertools::Itertools;
44
use rustc_ast::ast::{AttrKind, Attribute};
5+
use rustc_ast::token::CommentKind;
56
use rustc_data_structures::fx::FxHashSet;
67
use rustc_hir as hir;
78
use rustc_lint::{LateContext, LateLintPass};
@@ -249,62 +250,53 @@ fn lint_for_missing_headers<'tcx>(
249250
}
250251
}
251252

252-
/// Cleanup documentation decoration (`///` and such).
253+
/// Cleanup documentation decoration.
253254
///
254255
/// We can't use `rustc_ast::attr::AttributeMethods::with_desugared_doc` or
255256
/// `rustc_ast::parse::lexer::comments::strip_doc_comment_decoration` because we
256257
/// need to keep track of
257258
/// the spans but this function is inspired from the later.
258259
#[allow(clippy::cast_possible_truncation)]
259260
#[must_use]
260-
pub fn strip_doc_comment_decoration(comment: &str, span: Span) -> (String, Vec<(usize, Span)>) {
261+
pub fn strip_doc_comment_decoration(doc: &str, comment_kind: CommentKind, span: Span) -> (String, Vec<(usize, Span)>) {
261262
// one-line comments lose their prefix
262-
const ONELINERS: &[&str] = &["///!", "///", "//!", "//"];
263-
for prefix in ONELINERS {
264-
if comment.starts_with(*prefix) {
265-
let doc = &comment[prefix.len()..];
266-
let mut doc = doc.to_owned();
267-
doc.push('\n');
268-
return (
269-
doc.to_owned(),
270-
vec![(doc.len(), span.with_lo(span.lo() + BytePos(prefix.len() as u32)))],
271-
);
272-
}
263+
if comment_kind == CommentKind::Line {
264+
let mut doc = doc.to_owned();
265+
doc.push('\n');
266+
let len = doc.len();
267+
// +3 skips the opening delimiter
268+
return (doc, vec![(len, span.with_lo(span.lo() + BytePos(3)))]);
273269
}
274270

275-
if comment.starts_with("/*") {
276-
let doc = &comment[3..comment.len() - 2];
277-
let mut sizes = vec![];
278-
let mut contains_initial_stars = false;
279-
for line in doc.lines() {
280-
let offset = line.as_ptr() as usize - comment.as_ptr() as usize;
281-
debug_assert_eq!(offset as u32 as usize, offset);
282-
contains_initial_stars |= line.trim_start().starts_with('*');
283-
// +1 for the newline
284-
sizes.push((line.len() + 1, span.with_lo(span.lo() + BytePos(offset as u32))));
285-
}
286-
if !contains_initial_stars {
287-
return (doc.to_string(), sizes);
288-
}
289-
// remove the initial '*'s if any
290-
let mut no_stars = String::with_capacity(doc.len());
291-
for line in doc.lines() {
292-
let mut chars = line.chars();
293-
while let Some(c) = chars.next() {
294-
if c.is_whitespace() {
295-
no_stars.push(c);
296-
} else {
297-
no_stars.push(if c == '*' { ' ' } else { c });
298-
break;
299-
}
271+
let mut sizes = vec![];
272+
let mut contains_initial_stars = false;
273+
for line in doc.lines() {
274+
let offset = line.as_ptr() as usize - doc.as_ptr() as usize;
275+
debug_assert_eq!(offset as u32 as usize, offset);
276+
contains_initial_stars |= line.trim_start().starts_with('*');
277+
// +1 adds the newline, +3 skips the opening delimiter
278+
sizes.push((line.len() + 1, span.with_lo(span.lo() + BytePos(3 + offset as u32))));
279+
}
280+
if !contains_initial_stars {
281+
return (doc.to_string(), sizes);
282+
}
283+
// remove the initial '*'s if any
284+
let mut no_stars = String::with_capacity(doc.len());
285+
for line in doc.lines() {
286+
let mut chars = line.chars();
287+
while let Some(c) = chars.next() {
288+
if c.is_whitespace() {
289+
no_stars.push(c);
290+
} else {
291+
no_stars.push(if c == '*' { ' ' } else { c });
292+
break;
300293
}
301-
no_stars.push_str(chars.as_str());
302-
no_stars.push('\n');
303294
}
304-
return (no_stars, sizes);
295+
no_stars.push_str(chars.as_str());
296+
no_stars.push('\n');
305297
}
306298

307-
panic!("not a doc-comment: {}", comment);
299+
(no_stars, sizes)
308300
}
309301

310302
#[derive(Copy, Clone)]
@@ -318,9 +310,8 @@ fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs
318310
let mut spans = vec![];
319311

320312
for attr in attrs {
321-
if let AttrKind::DocComment(ref comment) = attr.kind {
322-
let comment = comment.to_string();
323-
let (comment, current_spans) = strip_doc_comment_decoration(&comment, attr.span);
313+
if let AttrKind::DocComment(comment_kind, comment) = attr.kind {
314+
let (comment, current_spans) = strip_doc_comment_decoration(&comment.as_str(), comment_kind, attr.span);
324315
spans.extend_from_slice(&current_spans);
325316
doc.push_str(&comment);
326317
} else if attr.has_name(sym!(doc)) {

clippy_lints/src/functions.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
239239
return;
240240
}
241241
if cx.access_levels.is_exported(item.hir_id)
242-
&& !is_proc_macro(&item.attrs)
242+
&& !is_proc_macro(cx.sess(), &item.attrs)
243243
&& attr_by_name(&item.attrs, "no_mangle").is_none()
244244
{
245245
check_must_use_candidate(
@@ -262,7 +262,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
262262
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
263263
check_needless_must_use(cx, &sig.decl, item.hir_id, item.span, fn_header_span, attr);
264264
} else if cx.access_levels.is_exported(item.hir_id)
265-
&& !is_proc_macro(&item.attrs)
265+
&& !is_proc_macro(cx.sess(), &item.attrs)
266266
&& trait_ref_of_method(cx, item.hir_id).is_none()
267267
{
268268
check_must_use_candidate(
@@ -294,7 +294,8 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
294294
let body = cx.tcx.hir().body(eid);
295295
Self::check_raw_ptr(cx, sig.header.unsafety, &sig.decl, body, item.hir_id);
296296

297-
if attr.is_none() && cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(&item.attrs) {
297+
if attr.is_none() && cx.access_levels.is_exported(item.hir_id) && !is_proc_macro(cx.sess(), &item.attrs)
298+
{
298299
check_must_use_candidate(
299300
cx,
300301
&sig.decl,

clippy_lints/src/manual_non_exhaustive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn check_manual_non_exhaustive_enum(cx: &EarlyContext<'_>, item: &Item, variants
102102
"this seems like a manual implementation of the non-exhaustive pattern",
103103
|diag| {
104104
if_chain! {
105-
if !attr::contains_name(&item.attrs, sym!(non_exhaustive));
105+
if !item.attrs.iter().any(|attr| attr.has_name(sym!(non_exhaustive)));
106106
let header_span = cx.sess.source_map().span_until_char(item.span, '{');
107107
if let Some(snippet) = snippet_opt(cx, header_span);
108108
then {
@@ -154,7 +154,7 @@ fn check_manual_non_exhaustive_struct(cx: &EarlyContext<'_>, item: &Item, data:
154154
"this seems like a manual implementation of the non-exhaustive pattern",
155155
|diag| {
156156
if_chain! {
157-
if !attr::contains_name(&item.attrs, sym!(non_exhaustive));
157+
if !item.attrs.iter().any(|attr| attr.has_name(sym!(non_exhaustive)));
158158
let header_span = find_header_span(cx, item, data);
159159
if let Some(snippet) = snippet_opt(cx, header_span);
160160
then {

clippy_lints/src/non_expressive_names.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::utils::{span_lint, span_lint_and_then};
22
use rustc_ast::ast::{
33
Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, Item, ItemKind, Local, MacCall, Pat, PatKind,
44
};
5-
use rustc_ast::attr;
65
use rustc_ast::visit::{walk_block, walk_expr, walk_pat, Visitor};
76
use rustc_lint::{EarlyContext, EarlyLintPass};
87
use rustc_middle::lint::in_external_macro;
@@ -385,7 +384,7 @@ impl EarlyLintPass for NonExpressiveNames {
385384
}
386385

387386
fn do_check(lint: &mut NonExpressiveNames, cx: &EarlyContext<'_>, attrs: &[Attribute], decl: &FnDecl, blk: &Block) {
388-
if !attr::contains_name(attrs, sym!(test)) {
387+
if !attrs.iter().any(|attr| attr.has_name(sym!(test))) {
389388
let mut visitor = SimilarNamesLocalVisitor {
390389
names: Vec::new(),
391390
cx,

clippy_lints/src/tabs_in_doc_comments.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,14 @@ declare_lint_pass!(TabsInDocComments => [TABS_IN_DOC_COMMENTS]);
6060

6161
impl TabsInDocComments {
6262
fn warn_if_tabs_in_doc(cx: &EarlyContext<'_>, attr: &ast::Attribute) {
63-
if let ast::AttrKind::DocComment(comment) = attr.kind {
63+
if let ast::AttrKind::DocComment(_, comment) = attr.kind {
6464
let comment = comment.as_str();
6565

6666
for (lo, hi) in get_chunks_of_tabs(&comment) {
67+
// +3 skips the opening delimiter
6768
let new_span = Span::new(
68-
attr.span.lo() + BytePos(lo),
69-
attr.span.lo() + BytePos(hi),
69+
attr.span.lo() + BytePos(3 + lo),
70+
attr.span.lo() + BytePos(3 + hi),
7071
attr.span.ctxt(),
7172
);
7273
span_lint_and_sugg(

clippy_lints/src/utils/ast_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
506506
use AttrKind::*;
507507
l.style == r.style
508508
&& match (&l.kind, &r.kind) {
509-
(DocComment(l), DocComment(r)) => l == r,
509+
(DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
510510
(Normal(l), Normal(r)) => eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args),
511511
_ => false,
512512
}

clippy_lints/src/utils/attrs.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_ast::ast;
2-
use rustc_ast::expand::is_proc_macro_attr;
32
use rustc_errors::Applicability;
43
use rustc_session::Session;
54
use std::str::FromStr;
@@ -126,6 +125,6 @@ fn parse_attrs<F: FnMut(u64)>(sess: &Session, attrs: &[ast::Attribute], name: &'
126125

127126
/// Return true if the attributes contain any of `proc_macro`,
128127
/// `proc_macro_derive` or `proc_macro_attribute`, false otherwise
129-
pub fn is_proc_macro(attrs: &[ast::Attribute]) -> bool {
130-
attrs.iter().any(is_proc_macro_attr)
128+
pub fn is_proc_macro(sess: &Session, attrs: &[ast::Attribute]) -> bool {
129+
attrs.iter().any(|attr| sess.is_proc_macro_attr(attr))
131130
}

clippy_lints/src/utils/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ pub fn is_refutable(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
931931
/// Checks for the `#[automatically_derived]` attribute all `#[derive]`d
932932
/// implementations have.
933933
pub fn is_automatically_derived(attrs: &[ast::Attribute]) -> bool {
934-
attr::contains_name(attrs, sym!(automatically_derived))
934+
attrs.iter().any(|attr| attr.has_name(sym!(automatically_derived)))
935935
}
936936

937937
/// Remove blocks around an expression.

tests/ui/indexing_slicing_index.stderr

+1-21
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
error: this operation will panic at runtime
2-
--> $DIR/indexing_slicing_index.rs:11:5
3-
|
4-
LL | x[4]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
5-
| ^^^^ index out of bounds: the len is 4 but the index is 4
6-
|
7-
= note: `#[deny(unconditional_panic)]` on by default
8-
9-
error: this operation will panic at runtime
10-
--> $DIR/indexing_slicing_index.rs:12:5
11-
|
12-
LL | x[1 << 3]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
13-
| ^^^^^^^^^ index out of bounds: the len is 4 but the index is 8
14-
15-
error: this operation will panic at runtime
16-
--> $DIR/indexing_slicing_index.rs:27:5
17-
|
18-
LL | x[N]; // Ok, let rustc's `const_err` lint handle `usize` indexing on arrays.
19-
| ^^^^ index out of bounds: the len is 4 but the index is 15
20-
211
error: indexing may panic.
222
--> $DIR/indexing_slicing_index.rs:10:5
233
|
@@ -75,5 +55,5 @@ LL | v[M];
7555
|
7656
= help: Consider using `.get(n)` or `.get_mut(n)` instead
7757

78-
error: aborting due to 10 previous errors
58+
error: aborting due to 7 previous errors
7959

0 commit comments

Comments
 (0)