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