@@ -3,43 +3,19 @@ use clippy_utils::paths;
33use clippy_utils:: ty:: match_type;
44use rustc_ast:: ast:: LitKind ;
55use rustc_hir:: { Expr , ExprKind } ;
6- use rustc_lint:: { LateContext , LateLintPass } ;
7- use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
6+ use rustc_lint:: LateContext ;
87use rustc_span:: source_map:: { Span , Spanned } ;
98
10- declare_clippy_lint ! {
11- /// ### What it does
12- /// Checks for duplicate open options as well as combinations
13- /// that make no sense.
14- ///
15- /// ### Why is this bad?
16- /// In the best case, the code will be harder to read than
17- /// necessary. I don't know the worst case.
18- ///
19- /// ### Example
20- /// ```rust
21- /// use std::fs::OpenOptions;
22- ///
23- /// OpenOptions::new().read(true).truncate(true);
24- /// ```
25- #[ clippy:: version = "pre 1.29.0" ]
26- pub NONSENSICAL_OPEN_OPTIONS ,
27- correctness,
28- "nonsensical combination of options for opening a file"
29- }
30-
31- declare_lint_pass ! ( OpenOptions => [ NONSENSICAL_OPEN_OPTIONS ] ) ;
9+ use super :: NONSENSICAL_OPEN_OPTIONS ;
3210
33- impl < ' tcx > LateLintPass < ' tcx > for OpenOptions {
34- fn check_expr ( & mut self , cx : & LateContext < ' tcx > , e : & ' tcx Expr < ' _ > ) {
35- if let ExprKind :: MethodCall ( path, [ self_arg, ..] , _) = & e. kind {
36- let obj_ty = cx. typeck_results ( ) . expr_ty ( self_arg) . peel_refs ( ) ;
37- if path. ident . name == sym ! ( open) && match_type ( cx, obj_ty, & paths:: OPEN_OPTIONS ) {
38- let mut options = Vec :: new ( ) ;
39- get_open_options ( cx, self_arg, & mut options) ;
40- check_open_options ( cx, & options, e. span ) ;
41- }
42- }
11+ pub ( super ) fn check < ' tcx > ( cx : & LateContext < ' tcx > , e : & ' tcx Expr < ' _ > , recv : & ' tcx Expr < ' _ > ) {
12+ if let Some ( method_id) = cx. typeck_results ( ) . type_dependent_def_id ( e. hir_id )
13+ && let Some ( impl_id) = cx. tcx . impl_of_method ( method_id)
14+ && match_type ( cx, cx. tcx . type_of ( impl_id) , & paths:: OPEN_OPTIONS )
15+ {
16+ let mut options = Vec :: new ( ) ;
17+ get_open_options ( cx, recv, & mut options) ;
18+ check_open_options ( cx, & options, e. span ) ;
4319 }
4420}
4521
0 commit comments