Skip to content

Commit a45af45

Browse files
borsAndre Bogus
authored and
Andre Bogus
committed
Auto merge of #5259 - flip1995:lang_items, r=phansch
Use lang items instead of get_trait_def_id where possible changelog: none
2 parents f44181e + 91042db commit a45af45

File tree

7 files changed

+48
-15
lines changed

7 files changed

+48
-15
lines changed

clippy_lints/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ semver = "0.9.0"
3333
# NOTE: cargo requires serde feat in its url dep
3434
# see <https://github.com/rust-lang/rust/pull/63587#issuecomment-522343864>
3535
url = { version = "2.1.0", features = ["serde"] }
36+
bytecount = "0.6.0"
3637

3738
[features]
3839
deny-warnings = []

clippy_lints/src/doc.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::utils::{get_trait_def_id, implements_trait, is_entrypoint_fn, match_type, paths, return_ty, span_lint};
1+
use crate::utils::{implements_trait, is_entrypoint_fn, match_type, paths, return_ty, snippet_opt, span_lint};
2+
use bytecount::count;
23
use if_chain::if_chain;
34
use itertools::Itertools;
45
use rustc::lint::in_external_macro;
@@ -227,7 +228,7 @@ fn lint_for_missing_headers<'a, 'tcx>(
227228
} else {
228229
if_chain! {
229230
if let Some(body_id) = body_id;
230-
if let Some(future) = get_trait_def_id(cx, &paths::FUTURE);
231+
if let Some(future) = cx.tcx.lang_items().future_trait();
231232
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
232233
let mir = cx.tcx.optimized_mir(def_id);
233234
let ret_ty = mir.return_ty();
@@ -357,9 +358,9 @@ fn check_attrs<'a>(cx: &LateContext<'_, '_>, valid_idents: &FxHashSet<String>, a
357358

358359
match (previous.0, current.0) {
359360
(Text(previous), Text(current)) => {
360-
let mut previous = previous.to_string();
361-
previous.push_str(&current);
362-
Ok((Text(previous.into()), previous_range))
361+
let text = (previous.to_string() + &current).into();
362+
let range = previous_range.start..current_range.end;
363+
Ok((Text(text), range))
363364
},
364365
(previous, current) => Err(((previous, previous_range), (current, current_range))),
365366
}
@@ -413,6 +414,16 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
413414
};
414415
let (begin, span) = spans[index];
415416
if in_code {
417+
let lo = span.lo() + BytePos::from_usize(range.start - begin);
418+
// we need a span that includes at least the code.
419+
// however, because we have already stripped the comment
420+
// prefixes, we need to add their size back. We currently
421+
// do this in quite a hacky way: count the lines and multiply by 4.
422+
let span = Span::new(
423+
lo,
424+
lo + BytePos::from_usize(text.len() + count(text.as_bytes(), b'\n') * 4),
425+
span.ctxt(),
426+
);
416427
check_code(cx, &text, span);
417428
} else {
418429
// Adjust for the beginning of the current `Event`
@@ -429,8 +440,14 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
429440
static LEAVE_MAIN_PATTERNS: &[&str] = &["static", "fn main() {}", "extern crate", "async fn main() {"];
430441

431442
fn check_code(cx: &LateContext<'_, '_>, text: &str, span: Span) {
432-
if text.contains("fn main() {") && !LEAVE_MAIN_PATTERNS.iter().any(|p| text.contains(p)) {
433-
span_lint(cx, NEEDLESS_DOCTEST_MAIN, span, "needless `fn main` in doctest");
443+
if let Some(comment) = snippet_opt(cx, span) {
444+
if let Some(offset) = comment.find("fn main() {") {
445+
if !LEAVE_MAIN_PATTERNS.iter().any(|p| text.contains(p)) {
446+
let lo = span.lo() + BytePos::from_usize(offset);
447+
let span = Span::new(lo, lo + BytePos::from_usize("fn main()".len()), span.ctxt());
448+
span_lint(cx, NEEDLESS_DOCTEST_MAIN, span, "needless `fn main` in doctest");
449+
}
450+
}
434451
}
435452
}
436453

clippy_lints/src/inherent_to_string.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InherentToString {
120120
}
121121

122122
fn show_lint(cx: &LateContext<'_, '_>, item: &ImplItem<'_>) {
123-
let display_trait_id =
124-
get_trait_def_id(cx, &["core", "fmt", "Display"]).expect("Failed to get trait ID of `Display`!");
123+
let display_trait_id = get_trait_def_id(cx, &paths::DISPLAY_TRAIT).expect("Failed to get trait ID of `Display`!");
125124

126125
// Get the real type of 'self'
127126
let fn_def_id = cx.tcx.hir().local_def_id(item.hir_id);

clippy_lints/src/neg_cmp_op_on_partial_ord.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NoNegCompOpForPartialOrd {
6767
};
6868

6969
let implements_partial_ord = {
70-
if let Some(id) = utils::get_trait_def_id(cx, &paths::PARTIAL_ORD) {
70+
if let Some(id) = cx.tcx.lang_items().partial_ord_trait() {
7171
utils::implements_trait(cx, ty, id, &[])
7272
} else {
7373
return;

clippy_lints/src/utils/paths.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub const DEFAULT_TRAIT_METHOD: [&str; 4] = ["core", "default", "Default", "defa
2424
pub const DEREF_MUT_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "DerefMut", "deref_mut"];
2525
pub const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", "deref"];
2626
pub const DISPLAY_FMT_METHOD: [&str; 4] = ["core", "fmt", "Display", "fmt"];
27+
pub const DISPLAY_TRAIT: [&str; 3] = ["core", "fmt", "Display"];
2728
pub const DOUBLE_ENDED_ITERATOR: [&str; 4] = ["core", "iter", "traits", "DoubleEndedIterator"];
2829
pub const DROP: [&str; 3] = ["core", "mem", "drop"];
2930
pub const DROP_TRAIT: [&str; 4] = ["core", "ops", "drop", "Drop"];
@@ -36,7 +37,6 @@ pub const FMT_ARGUMENTS_NEW_V1_FORMATTED: [&str; 4] = ["core", "fmt", "Arguments
3637
pub const FMT_ARGUMENTV1_NEW: [&str; 4] = ["core", "fmt", "ArgumentV1", "new"];
3738
pub const FROM_FROM: [&str; 4] = ["core", "convert", "From", "from"];
3839
pub const FROM_TRAIT: [&str; 3] = ["core", "convert", "From"];
39-
pub const FUTURE: [&str; 3] = ["std", "future", "Future"];
4040
pub const HASH: [&str; 2] = ["hash", "Hash"];
4141
pub const HASHMAP: [&str; 5] = ["std", "collections", "hash", "map", "HashMap"];
4242
pub const HASHMAP_ENTRY: [&str; 5] = ["std", "collections", "hash", "map", "Entry"];
@@ -69,7 +69,6 @@ pub const ORD: [&str; 3] = ["core", "cmp", "Ord"];
6969
pub const OS_STRING: [&str; 4] = ["std", "ffi", "os_str", "OsString"];
7070
pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"];
7171
pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"];
72-
pub const PARTIAL_ORD: [&str; 3] = ["core", "cmp", "PartialOrd"];
7372
pub const PATH: [&str; 3] = ["std", "path", "Path"];
7473
pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
7574
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];

tests/ui/needless_doc_main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
/// unimplemented!();
99
/// }
1010
/// ```
11+
///
12+
/// This should also lint, and have correct span
13+
///
14+
/// ```
15+
/// use std::io::Write as _;
16+
///
17+
/// fn main() {
18+
/// let x = String::new();
19+
/// write!(x, "Hallo");
20+
/// }
21+
/// ```
1122
fn bad_doctest() {}
1223

1324
/// # Examples

tests/ui/needless_doc_main.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
error: needless `fn main` in doctest
2-
--> $DIR/needless_doc_main.rs:7:4
2+
--> $DIR/needless_doc_main.rs:7:5
33
|
44
LL | /// fn main() {
5-
| ^^^^^^^^^^^^
5+
| ^^^^^^^^^
66
|
77
= note: `-D clippy::needless-doctest-main` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: needless `fn main` in doctest
10+
--> $DIR/needless_doc_main.rs:17:5
11+
|
12+
LL | /// fn main() {
13+
| ^^^^^^^^^
14+
15+
error: aborting due to 2 previous errors
1016

0 commit comments

Comments
 (0)