@@ -423,7 +423,12 @@ declare_clippy_lint! {
423
423
/// **Why is this bad?** It's more concise and clear to just use the proper
424
424
/// utility function
425
425
///
426
- /// **Known problems:** None.
426
+ /// **Known problems:** This will change the drop order for the matched type. Both `if let` and
427
+ /// `while let` will drop the value at the end of the block, both `if` and `while` will drop the
428
+ /// value before entering the block. For most types this change will not matter, but for a few
429
+ /// types this will not be an acceptable change (e.g. locks). See the
430
+ /// [reference](https://doc.rust-lang.org/reference/destructors.html#drop-scopes) for more about
431
+ /// drop order.
427
432
///
428
433
/// **Example:**
429
434
///
@@ -1703,55 +1708,206 @@ where
1703
1708
mod redundant_pattern_match {
1704
1709
use super :: REDUNDANT_PATTERN_MATCHING ;
1705
1710
use clippy_utils:: diagnostics:: span_lint_and_then;
1706
- use clippy_utils:: source:: snippet;
1711
+ use clippy_utils:: source:: { snippet, snippet_with_applicability} ;
1712
+ use clippy_utils:: ty:: { implements_trait, is_type_diagnostic_item, is_type_lang_item, match_type} ;
1707
1713
use clippy_utils:: { is_lang_ctor, is_qpath_def_path, is_trait_method, paths} ;
1708
1714
use if_chain:: if_chain;
1709
1715
use rustc_ast:: ast:: LitKind ;
1710
1716
use rustc_errors:: Applicability ;
1711
1717
use rustc_hir:: LangItem :: { OptionNone , OptionSome , PollPending , PollReady , ResultErr , ResultOk } ;
1712
- use rustc_hir:: { Arm , Expr , ExprKind , MatchSource , PatKind , QPath } ;
1718
+ use rustc_hir:: {
1719
+ intravisit:: { walk_expr, ErasedMap , NestedVisitorMap , Visitor } ,
1720
+ Arm , Block , Expr , ExprKind , LangItem , MatchSource , Node , PatKind , QPath ,
1721
+ } ;
1713
1722
use rustc_lint:: LateContext ;
1723
+ use rustc_middle:: ty:: { self , subst:: GenericArgKind , Ty } ;
1714
1724
use rustc_span:: sym;
1715
1725
1716
1726
pub fn check < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
1717
1727
if let ExprKind :: Match ( op, arms, ref match_source) = & expr. kind {
1718
1728
match match_source {
1719
1729
MatchSource :: Normal => find_sugg_for_match ( cx, expr, op, arms) ,
1720
- MatchSource :: IfLetDesugar { .. } => find_sugg_for_if_let ( cx, expr, op, arms, "if" ) ,
1721
- MatchSource :: WhileLetDesugar => find_sugg_for_if_let ( cx, expr, op, arms, "while" ) ,
1730
+ MatchSource :: IfLetDesugar { contains_else_clause } => {
1731
+ find_sugg_for_if_let ( cx, expr, op, & arms[ 0 ] , "if" , * contains_else_clause)
1732
+ } ,
1733
+ MatchSource :: WhileLetDesugar => find_sugg_for_if_let ( cx, expr, op, & arms[ 0 ] , "while" , false ) ,
1722
1734
_ => { } ,
1723
1735
}
1724
1736
}
1725
1737
}
1726
1738
1739
+ /// Checks if the drop order for a type matters. Some std types implement drop solely to
1740
+ /// deallocate memory. For these types, and composites containing them, changing the drop order
1741
+ /// won't result in any observable side effects.
1742
+ fn type_needs_ordered_drop ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> bool {
1743
+ if !ty. needs_drop ( cx. tcx , cx. param_env ) {
1744
+ false
1745
+ } else if !cx
1746
+ . tcx
1747
+ . lang_items ( )
1748
+ . drop_trait ( )
1749
+ . map_or ( false , |id| implements_trait ( cx, ty, id, & [ ] ) )
1750
+ {
1751
+ // This type doesn't implement drop, so no side effects here.
1752
+ // Check if any component type has any.
1753
+ match ty. kind ( ) {
1754
+ ty:: Tuple ( _) => ty. tuple_fields ( ) . any ( |ty| type_needs_ordered_drop ( cx, ty) ) ,
1755
+ ty:: Array ( ty, _) => type_needs_ordered_drop ( cx, ty) ,
1756
+ ty:: Adt ( adt, subs) => adt
1757
+ . all_fields ( )
1758
+ . map ( |f| f. ty ( cx. tcx , subs) )
1759
+ . any ( |ty| type_needs_ordered_drop ( cx, ty) ) ,
1760
+ _ => true ,
1761
+ }
1762
+ }
1763
+ // Check for std types which implement drop, but only for memory allocation.
1764
+ else if is_type_diagnostic_item ( cx, ty, sym:: vec_type)
1765
+ || is_type_lang_item ( cx, ty, LangItem :: OwnedBox )
1766
+ || is_type_diagnostic_item ( cx, ty, sym:: Rc )
1767
+ || is_type_diagnostic_item ( cx, ty, sym:: Arc )
1768
+ || is_type_diagnostic_item ( cx, ty, sym:: cstring_type)
1769
+ || match_type ( cx, ty, & paths:: BTREEMAP )
1770
+ || match_type ( cx, ty, & paths:: LINKED_LIST )
1771
+ || match_type ( cx, ty, & paths:: WEAK_RC )
1772
+ || match_type ( cx, ty, & paths:: WEAK_ARC )
1773
+ {
1774
+ // Check all of the generic arguments.
1775
+ if let ty:: Adt ( _, subs) = ty. kind ( ) {
1776
+ subs. types ( ) . any ( |ty| type_needs_ordered_drop ( cx, ty) )
1777
+ } else {
1778
+ true
1779
+ }
1780
+ } else {
1781
+ true
1782
+ }
1783
+ }
1784
+
1785
+ // Extract the generic arguments out of a type
1786
+ fn try_get_generic_ty ( ty : Ty < ' _ > , index : usize ) -> Option < Ty < ' _ > > {
1787
+ if_chain ! {
1788
+ if let ty:: Adt ( _, subs) = ty. kind( ) ;
1789
+ if let Some ( sub) = subs. get( index) ;
1790
+ if let GenericArgKind :: Type ( sub_ty) = sub. unpack( ) ;
1791
+ then {
1792
+ Some ( sub_ty)
1793
+ } else {
1794
+ None
1795
+ }
1796
+ }
1797
+ }
1798
+
1799
+ // Checks if there are any temporaries created in the given expression for which drop order
1800
+ // matters.
1801
+ fn temporaries_need_ordered_drop ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) -> bool {
1802
+ struct V < ' a , ' tcx > {
1803
+ cx : & ' a LateContext < ' tcx > ,
1804
+ res : bool ,
1805
+ }
1806
+ impl < ' a , ' tcx > Visitor < ' tcx > for V < ' a , ' tcx > {
1807
+ type Map = ErasedMap < ' tcx > ;
1808
+ fn nested_visit_map ( & mut self ) -> NestedVisitorMap < Self :: Map > {
1809
+ NestedVisitorMap :: None
1810
+ }
1811
+
1812
+ fn visit_expr ( & mut self , expr : & ' tcx Expr < ' tcx > ) {
1813
+ match expr. kind {
1814
+ // Taking the reference of a value leaves a temporary
1815
+ // e.g. In `&String::new()` the string is a temporary value.
1816
+ // Remaining fields are temporary values
1817
+ // e.g. In `(String::new(), 0).1` the string is a temporary value.
1818
+ ExprKind :: AddrOf ( _, _, expr) | ExprKind :: Field ( expr, _) => {
1819
+ if !matches ! ( expr. kind, ExprKind :: Path ( _) ) {
1820
+ if type_needs_ordered_drop ( self . cx , self . cx . typeck_results ( ) . expr_ty ( expr) ) {
1821
+ self . res = true ;
1822
+ } else {
1823
+ self . visit_expr ( expr) ;
1824
+ }
1825
+ }
1826
+ } ,
1827
+ // the base type is alway taken by reference.
1828
+ // e.g. In `(vec![0])[0]` the vector is a temporary value.
1829
+ ExprKind :: Index ( base, index) => {
1830
+ if !matches ! ( base. kind, ExprKind :: Path ( _) ) {
1831
+ if type_needs_ordered_drop ( self . cx , self . cx . typeck_results ( ) . expr_ty ( base) ) {
1832
+ self . res = true ;
1833
+ } else {
1834
+ self . visit_expr ( base) ;
1835
+ }
1836
+ }
1837
+ self . visit_expr ( index) ;
1838
+ } ,
1839
+ // Method calls can take self by reference.
1840
+ // e.g. In `String::new().len()` the string is a temporary value.
1841
+ ExprKind :: MethodCall ( _, _, [ self_arg, args @ ..] , _) => {
1842
+ if !matches ! ( self_arg. kind, ExprKind :: Path ( _) ) {
1843
+ let self_by_ref = self
1844
+ . cx
1845
+ . typeck_results ( )
1846
+ . type_dependent_def_id ( expr. hir_id )
1847
+ . map_or ( false , |id| self . cx . tcx . fn_sig ( id) . skip_binder ( ) . inputs ( ) [ 0 ] . is_ref ( ) ) ;
1848
+ if self_by_ref
1849
+ && type_needs_ordered_drop ( self . cx , self . cx . typeck_results ( ) . expr_ty ( self_arg) )
1850
+ {
1851
+ self . res = true ;
1852
+ } else {
1853
+ self . visit_expr ( self_arg)
1854
+ }
1855
+ }
1856
+ args. iter ( ) . for_each ( |arg| self . visit_expr ( arg) ) ;
1857
+ } ,
1858
+ // Either explicitly drops values, or changes control flow.
1859
+ ExprKind :: DropTemps ( _)
1860
+ | ExprKind :: Ret ( _)
1861
+ | ExprKind :: Break ( ..)
1862
+ | ExprKind :: Yield ( ..)
1863
+ | ExprKind :: Block ( Block { expr : None , .. } , _)
1864
+ | ExprKind :: Loop ( ..) => ( ) ,
1865
+
1866
+ // Only consider the final expression.
1867
+ ExprKind :: Block ( Block { expr : Some ( expr) , .. } , _) => self . visit_expr ( expr) ,
1868
+
1869
+ _ => walk_expr ( self , expr) ,
1870
+ }
1871
+ }
1872
+ }
1873
+
1874
+ let mut v = V { cx, res : false } ;
1875
+ v. visit_expr ( expr) ;
1876
+ v. res
1877
+ }
1878
+
1727
1879
fn find_sugg_for_if_let < ' tcx > (
1728
1880
cx : & LateContext < ' tcx > ,
1729
1881
expr : & ' tcx Expr < ' _ > ,
1730
- op : & Expr < ' _ > ,
1731
- arms : & [ Arm < ' _ > ] ,
1882
+ op : & ' tcx Expr < ' tcx > ,
1883
+ arm : & Arm < ' _ > ,
1732
1884
keyword : & ' static str ,
1885
+ has_else : bool ,
1733
1886
) {
1734
1887
// also look inside refs
1735
- let mut kind = & arms [ 0 ] . pat . kind ;
1888
+ let mut kind = & arm . pat . kind ;
1736
1889
// if we have &None for example, peel it so we can detect "if let None = x"
1737
1890
if let PatKind :: Ref ( inner, _mutability) = kind {
1738
1891
kind = & inner. kind ;
1739
1892
}
1740
- let good_method = match kind {
1893
+ let op_ty = cx. typeck_results ( ) . expr_ty ( op) ;
1894
+ // Determine which function should be used, and the type contained by the corresponding
1895
+ // variant.
1896
+ let ( good_method, inner_ty) = match kind {
1741
1897
PatKind :: TupleStruct ( ref path, [ sub_pat] , _) => {
1742
1898
if let PatKind :: Wild = sub_pat. kind {
1743
1899
if is_lang_ctor ( cx, path, ResultOk ) {
1744
- "is_ok()"
1900
+ ( "is_ok()" , try_get_generic_ty ( op_ty , 0 ) . unwrap_or ( op_ty ) )
1745
1901
} else if is_lang_ctor ( cx, path, ResultErr ) {
1746
- "is_err()"
1902
+ ( "is_err()" , try_get_generic_ty ( op_ty , 1 ) . unwrap_or ( op_ty ) )
1747
1903
} else if is_lang_ctor ( cx, path, OptionSome ) {
1748
- "is_some()"
1904
+ ( "is_some()" , op_ty )
1749
1905
} else if is_lang_ctor ( cx, path, PollReady ) {
1750
- "is_ready()"
1906
+ ( "is_ready()" , op_ty )
1751
1907
} else if is_qpath_def_path ( cx, path, sub_pat. hir_id , & paths:: IPADDR_V4 ) {
1752
- "is_ipv4()"
1908
+ ( "is_ipv4()" , op_ty )
1753
1909
} else if is_qpath_def_path ( cx, path, sub_pat. hir_id , & paths:: IPADDR_V6 ) {
1754
- "is_ipv6()"
1910
+ ( "is_ipv6()" , op_ty )
1755
1911
} else {
1756
1912
return ;
1757
1913
}
@@ -1760,17 +1916,36 @@ mod redundant_pattern_match {
1760
1916
}
1761
1917
} ,
1762
1918
PatKind :: Path ( ref path) => {
1763
- if is_lang_ctor ( cx, path, OptionNone ) {
1919
+ let method = if is_lang_ctor ( cx, path, OptionNone ) {
1764
1920
"is_none()"
1765
1921
} else if is_lang_ctor ( cx, path, PollPending ) {
1766
1922
"is_pending()"
1767
1923
} else {
1768
1924
return ;
1769
- }
1925
+ } ;
1926
+ // `None` and `Pending` don't have an inner type.
1927
+ ( method, cx. tcx . types . unit )
1770
1928
} ,
1771
1929
_ => return ,
1772
1930
} ;
1773
1931
1932
+ // If this is the last expression in a block or there is an else clause then the whole
1933
+ // type needs to be considered, not just the inner type of the branch being matched on.
1934
+ // Note the last expression in a block is dropped after all local bindings.
1935
+ let check_ty = if has_else
1936
+ || ( keyword == "if" && matches ! ( cx. tcx. hir( ) . parent_iter( expr. hir_id) . next( ) , Some ( ( _, Node :: Block ( ..) ) ) ) )
1937
+ {
1938
+ op_ty
1939
+ } else {
1940
+ inner_ty
1941
+ } ;
1942
+
1943
+ // All temporaries created in the scrutinee expression are dropped at the same time as the
1944
+ // scrutinee would be, so they have to be considered as well.
1945
+ // e.g. in `if let Some(x) = foo.lock().unwrap().baz.as_ref() { .. }` the lock will be held
1946
+ // for the duration if body.
1947
+ let needs_drop = type_needs_ordered_drop ( cx, check_ty) || temporaries_need_ordered_drop ( cx, op) ;
1948
+
1774
1949
// check that `while_let_on_iterator` lint does not trigger
1775
1950
if_chain ! {
1776
1951
if keyword == "while" ;
@@ -1789,7 +1964,7 @@ mod redundant_pattern_match {
1789
1964
span_lint_and_then (
1790
1965
cx,
1791
1966
REDUNDANT_PATTERN_MATCHING ,
1792
- arms [ 0 ] . pat . span ,
1967
+ arm . pat . span ,
1793
1968
& format ! ( "redundant pattern matching, consider using `{}`" , good_method) ,
1794
1969
|diag| {
1795
1970
// while let ... = ... { ... }
@@ -1803,12 +1978,20 @@ mod redundant_pattern_match {
1803
1978
// while let ... = ... { ... }
1804
1979
// ^^^^^^^^^^^^^^^^^^^
1805
1980
let span = expr_span. until ( op_span. shrink_to_hi ( ) ) ;
1806
- diag. span_suggestion (
1807
- span,
1808
- "try this" ,
1809
- format ! ( "{} {}.{}" , keyword, snippet( cx, op_span, "_" ) , good_method) ,
1810
- Applicability :: MachineApplicable , // snippet
1811
- ) ;
1981
+
1982
+ let mut app = if needs_drop {
1983
+ Applicability :: MaybeIncorrect
1984
+ } else {
1985
+ Applicability :: MachineApplicable
1986
+ } ;
1987
+ let sugg = snippet_with_applicability ( cx, op_span, "_" , & mut app) ;
1988
+
1989
+ diag. span_suggestion ( span, "try this" , format ! ( "{} {}.{}" , keyword, sugg, good_method) , app) ;
1990
+
1991
+ if needs_drop {
1992
+ diag. note ( "this will change drop order of the result, as well as all temporaries" ) ;
1993
+ diag. note ( "add `#[allow(clippy::redundant_pattern_matching)]` if this is important" ) ;
1994
+ }
1812
1995
} ,
1813
1996
) ;
1814
1997
}
0 commit comments