+ module_unused :: Module -> Bool
+ module_unused mod = any (((==) mod) . fst) unused_imp_mods
+
+---------------------
+warnDuplicateImports :: [GlobalRdrElt] -> RnM ()
+-- Given the GREs for names that are used, figure out which imports
+-- could be omitted without changing the top-level environment.
+--
+-- NB: Given import Foo( T )
+-- import qualified Foo
+-- we do not report a duplicate import, even though Foo.T is brought
+-- into scope by both, because there's nothing you can *omit* without
+-- changing the top-level environment. So we complain only if it's
+-- explicitly named in both imports or neither.
+--
+-- Furthermore, we complain about Foo.T only if
+-- there is no complaint about (unqualified) T
+
+warnDuplicateImports gres
+ = ifOptM Opt_WarnUnusedImports $
+ sequenceM_ [ warn name pr
+ -- The 'head' picks the first offending group
+ -- for this particular name
+ | GRE { gre_name = name, gre_prov = Imported imps } <- gres
+ , pr <- redundants imps ]
+ where
+ warn name (red_imp, cov_imp)
+ = addWarnAt (importSpecLoc red_imp)
+ (vcat [ptext SLIT("Redundant import of:") <+> quotes pp_name,
+ ptext SLIT("It is also") <+> ppr cov_imp])
+ where
+ pp_name | is_qual red_decl = ppr (is_as red_decl) <> dot <> ppr occ
+ | otherwise = ppr occ
+ occ = nameOccName name
+ red_decl = is_decl red_imp
+
+ redundants :: [ImportSpec] -> [(ImportSpec,ImportSpec)]
+ -- The returned pair is (redundant-import, covering-import)
+ redundants imps
+ = [ (red_imp, cov_imp)
+ | red_imp <- imps
+ , cov_imp <- take 1 (filter (covers red_imp) imps) ]
+
+ -- "red_imp" is a putative redundant import
+ -- "cov_imp" potentially covers it
+ -- This test decides whether red_imp could be dropped
+ --
+ -- NOTE: currently the test does not warn about
+ -- import M( x )
+ -- imoprt N( x )
+ -- even if the same underlying 'x' is involved, because dropping
+ -- either import would change the qualified names in scope (M.x, N.x)
+ -- But if the qualified names aren't used, the import is indeed redundant
+ -- Sadly we don't know that. Oh well.
+ covers red_imp@(ImpSpec { is_decl = red_decl, is_item = red_item })
+ cov_imp@(ImpSpec { is_decl = cov_decl, is_item = cov_item })
+ | red_loc == cov_loc
+ = False -- Ignore diagonal elements
+ | not (is_as red_decl == is_as cov_decl)
+ = False -- They bring into scope different qualified names
+ | not (is_qual red_decl) && is_qual cov_decl
+ = False -- Covering one doesn't bring unqualified name into scope
+ | red_selective
+ = not cov_selective -- Redundant one is selective and covering one isn't
+ || red_later -- Both are explicit; tie-break using red_later
+ | otherwise
+ = not cov_selective -- Neither import is selective
+ && (is_mod red_decl == is_mod cov_decl) -- They import the same module
+ && red_later -- Tie-break
+ where
+ red_loc = importSpecLoc red_imp
+ cov_loc = importSpecLoc cov_imp
+ red_later = red_loc > cov_loc
+ cov_selective = selectiveImpItem cov_item
+ red_selective = selectiveImpItem red_item