Skip to content

Commit 8881504

Browse files
authored
Rollup merge of rust-lang#58805 - fabric-and-ink:redundant_import, r=petrochenkov
Lint for redundant imports This is an attempt at solving rust-lang#10178. The changes are suggested by `@petrochenkov`. I need some feedback on how to continue, since I get a lot of false positives in macro invocations in `libcore`. I suspect that the test ``` ... if directive.parent_scope.expansion == Mark::root() { return; } ... ``` is wrong. But I don't know how to check correctly if I am at a macro expansion root – given that this is the reason for the errors in the first place. Todos: - [x] Solve problem with false positives - [x] Turn hard error into warning - [x] Add unit tests Closes rust-lang#10178
2 parents 1dce36e + caa37d8 commit 8881504

File tree

30 files changed

+199
-42
lines changed

30 files changed

+199
-42
lines changed

src/liballoc/borrow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<T> ToOwned for T
135135
/// Another example showing how to keep `Cow` in a struct:
136136
///
137137
/// ```
138-
/// use std::borrow::{Cow, ToOwned};
138+
/// use std::borrow::Cow;
139139
///
140140
/// struct Items<'a, X: 'a> where [X]: ToOwned<Owned = Vec<X>> {
141141
/// values: Cow<'a, [X]>,

src/libcore/cell.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,6 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
14231423
///
14241424
/// ```
14251425
/// use std::cell::UnsafeCell;
1426-
/// use std::marker::Sync;
14271426
///
14281427
/// # #[allow(dead_code)]
14291428
/// struct NotThreadSafe<T> {

src/librustc/lint/builtin.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ pub enum BuiltinLintDiagnostics {
482482
UnknownCrateTypes(Span, String, String),
483483
UnusedImports(String, Vec<(Span, String)>),
484484
NestedImplTrait { outer_impl_trait_span: Span, inner_impl_trait_span: Span },
485+
RedundantImport(Vec<(Span, bool)>, ast::Ident),
485486
}
486487

487488
impl BuiltinLintDiagnostics {
@@ -578,6 +579,15 @@ impl BuiltinLintDiagnostics {
578579
db.span_label(outer_impl_trait_span, "outer `impl Trait`");
579580
db.span_label(inner_impl_trait_span, "nested `impl Trait` here");
580581
}
582+
BuiltinLintDiagnostics::RedundantImport(spans, ident) => {
583+
for (span, is_imported) in spans {
584+
let introduced = if is_imported { "imported" } else { "defined" };
585+
db.span_label(
586+
span,
587+
format!("the item `{}` is already {} here", ident, introduced)
588+
);
589+
}
590+
}
581591
}
582592
}
583593
}

src/librustc/ty/query/on_disk_cache.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,6 @@ impl<'enc, 'a, 'tcx, E> CacheEncoder<'enc, 'a, 'tcx, E>
778778
value: &V)
779779
-> Result<(), E::Error>
780780
{
781-
use crate::ty::codec::TyEncoder;
782781
let start_pos = self.position();
783782

784783
tag.encode(self)?;

src/librustc_codegen_llvm/context.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
377377
// Returns a Value of the "eh_unwind_resume" lang item if one is defined,
378378
// otherwise declares it as an external function.
379379
fn eh_unwind_resume(&self) -> &'ll Value {
380-
use crate::attributes;
381380
let unwresume = &self.eh_unwind_resume;
382381
if let Some(llfn) = unwresume.get() {
383382
return llfn;

src/librustc_codegen_ssa/mir/rvalue.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,6 @@ fn cast_int_to_float<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
750750
// All inputs greater or equal to (f32::MAX + 0.5 ULP) are rounded to infinity,
751751
// and for everything else LLVM's uitofp works just fine.
752752
use rustc_apfloat::ieee::Single;
753-
use rustc_apfloat::Float;
754753
const MAX_F32_PLUS_HALF_ULP: u128 = ((1 << (Single::PRECISION + 1)) - 1)
755754
<< (Single::MAX_EXP - Single::PRECISION as i16);
756755
let max = bx.cx().const_uint_big(int_ty, MAX_F32_PLUS_HALF_ULP);

src/librustc_errors/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl CodeSuggestion {
155155
/// Returns the assembled code suggestions and whether they should be shown with an underline.
156156
pub fn splice_lines(&self, cm: &SourceMapperDyn)
157157
-> Vec<(String, Vec<SubstitutionPart>)> {
158-
use syntax_pos::{CharPos, Loc, Pos};
158+
use syntax_pos::{CharPos, Pos};
159159

160160
fn push_trailing(buf: &mut String,
161161
line_opt: Option<&Cow<'_, str>>,

src/librustc_interface/profile/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ fn total_duration(traces: &[trace::Rec]) -> Duration {
6161
fn profile_queries_thread(r: Receiver<ProfileQueriesMsg>) {
6262
use self::trace::*;
6363
use std::fs::File;
64-
use std::time::{Instant};
6564

6665
let mut profq_msgs: Vec<ProfileQueriesMsg> = vec![];
6766
let mut frame: StackFrame = StackFrame { parse_st: ParseState::Clear, traces: vec![] };

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
427427

428428
let mut kind = match (lo, hi) {
429429
(PatternKind::Constant { value: lo }, PatternKind::Constant { value: hi }) => {
430-
use std::cmp::Ordering;
431430
let cmp = compare_const_vals(
432431
self.tcx,
433432
lo,

src/librustc_mir/interpret/operator.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,6 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
331331
val: ImmTy<'tcx, M::PointerTag>,
332332
) -> EvalResult<'tcx, Scalar<M::PointerTag>> {
333333
use rustc::mir::UnOp::*;
334-
use rustc_apfloat::ieee::{Single, Double};
335-
use rustc_apfloat::Float;
336334

337335
let layout = val.layout;
338336
let val = val.to_scalar()?;

0 commit comments

Comments
 (0)