3
3
use clippy_config:: Conf ;
4
4
use clippy_utils:: attrs:: is_doc_hidden;
5
5
use clippy_utils:: diagnostics:: { span_lint, span_lint_and_help, span_lint_and_then} ;
6
- use clippy_utils:: macros:: { is_panic, root_macro_call_first_node} ;
7
6
use clippy_utils:: source:: snippet_opt;
8
- use clippy_utils:: ty:: is_type_diagnostic_item;
9
- use clippy_utils:: visitors:: Visitable ;
10
- use clippy_utils:: { is_entrypoint_fn, is_trait_impl_item, method_chain_args} ;
7
+ use clippy_utils:: { is_entrypoint_fn, is_trait_impl_item} ;
11
8
use pulldown_cmark:: Event :: {
12
9
Code , DisplayMath , End , FootnoteReference , HardBreak , Html , InlineHtml , InlineMath , Rule , SoftBreak , Start ,
13
10
TaskListMarker , Text ,
@@ -16,18 +13,15 @@ use pulldown_cmark::Tag::{BlockQuote, CodeBlock, FootnoteDefinition, Heading, It
16
13
use pulldown_cmark:: { BrokenLink , CodeBlockKind , CowStr , Options , TagEnd } ;
17
14
use rustc_data_structures:: fx:: FxHashSet ;
18
15
use rustc_errors:: Applicability ;
19
- use rustc_hir:: intravisit:: { self , Visitor } ;
20
- use rustc_hir:: { AnonConst , Attribute , Expr , ImplItemKind , ItemKind , Node , Safety , TraitItemKind } ;
16
+ use rustc_hir:: { Attribute , ImplItemKind , ItemKind , Node , Safety , TraitItemKind } ;
21
17
use rustc_lint:: { EarlyContext , EarlyLintPass , LateContext , LateLintPass , LintContext } ;
22
- use rustc_middle:: hir:: nested_filter;
23
- use rustc_middle:: ty;
24
18
use rustc_resolve:: rustdoc:: {
25
19
DocFragment , add_doc_fragment, attrs_to_doc_fragments, main_body_opts, source_span_for_markdown_range,
26
20
span_of_fragments,
27
21
} ;
28
22
use rustc_session:: impl_lint_pass;
23
+ use rustc_span:: Span ;
29
24
use rustc_span:: edition:: Edition ;
30
- use rustc_span:: { Span , sym} ;
31
25
use std:: ops:: Range ;
32
26
use url:: Url ;
33
27
@@ -657,20 +651,16 @@ impl<'tcx> LateLintPass<'tcx> for Documentation {
657
651
self . check_private_items ,
658
652
) ;
659
653
match item. kind {
660
- ItemKind :: Fn { sig, body : body_id , .. } => {
654
+ ItemKind :: Fn { sig, body, .. } => {
661
655
if !( is_entrypoint_fn ( cx, item. owner_id . to_def_id ( ) )
662
656
|| item. span . in_external_macro ( cx. tcx . sess . source_map ( ) ) )
663
657
{
664
- let body = cx. tcx . hir_body ( body_id) ;
665
-
666
- let panic_info = FindPanicUnwrap :: find_span ( cx, cx. tcx . typeck ( item. owner_id ) , body. value ) ;
667
658
missing_headers:: check (
668
659
cx,
669
660
item. owner_id ,
670
661
sig,
671
662
headers,
672
- Some ( body_id) ,
673
- panic_info,
663
+ Some ( body) ,
674
664
self . check_private_items ,
675
665
) ;
676
666
}
@@ -697,32 +687,20 @@ impl<'tcx> LateLintPass<'tcx> for Documentation {
697
687
if let TraitItemKind :: Fn ( sig, ..) = trait_item. kind
698
688
&& !trait_item. span . in_external_macro ( cx. tcx . sess . source_map ( ) )
699
689
{
700
- missing_headers:: check (
701
- cx,
702
- trait_item. owner_id ,
703
- sig,
704
- headers,
705
- None ,
706
- None ,
707
- self . check_private_items ,
708
- ) ;
690
+ missing_headers:: check ( cx, trait_item. owner_id , sig, headers, None , self . check_private_items ) ;
709
691
}
710
692
} ,
711
693
Node :: ImplItem ( impl_item) => {
712
694
if let ImplItemKind :: Fn ( sig, body_id) = impl_item. kind
713
695
&& !impl_item. span . in_external_macro ( cx. tcx . sess . source_map ( ) )
714
696
&& !is_trait_impl_item ( cx, impl_item. hir_id ( ) )
715
697
{
716
- let body = cx. tcx . hir_body ( body_id) ;
717
-
718
- let panic_span = FindPanicUnwrap :: find_span ( cx, cx. tcx . typeck ( impl_item. owner_id ) , body. value ) ;
719
698
missing_headers:: check (
720
699
cx,
721
700
impl_item. owner_id ,
722
701
sig,
723
702
headers,
724
703
Some ( body_id) ,
725
- panic_span,
726
704
self . check_private_items ,
727
705
) ;
728
706
}
@@ -1169,72 +1147,6 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
1169
1147
headers
1170
1148
}
1171
1149
1172
- struct FindPanicUnwrap < ' a , ' tcx > {
1173
- cx : & ' a LateContext < ' tcx > ,
1174
- is_const : bool ,
1175
- panic_span : Option < Span > ,
1176
- typeck_results : & ' tcx ty:: TypeckResults < ' tcx > ,
1177
- }
1178
-
1179
- impl < ' a , ' tcx > FindPanicUnwrap < ' a , ' tcx > {
1180
- pub fn find_span (
1181
- cx : & ' a LateContext < ' tcx > ,
1182
- typeck_results : & ' tcx ty:: TypeckResults < ' tcx > ,
1183
- body : impl Visitable < ' tcx > ,
1184
- ) -> Option < ( Span , bool ) > {
1185
- let mut vis = Self {
1186
- cx,
1187
- is_const : false ,
1188
- panic_span : None ,
1189
- typeck_results,
1190
- } ;
1191
- body. visit ( & mut vis) ;
1192
- vis. panic_span . map ( |el| ( el, vis. is_const ) )
1193
- }
1194
- }
1195
-
1196
- impl < ' tcx > Visitor < ' tcx > for FindPanicUnwrap < ' _ , ' tcx > {
1197
- type NestedFilter = nested_filter:: OnlyBodies ;
1198
-
1199
- fn visit_expr ( & mut self , expr : & ' tcx Expr < ' _ > ) {
1200
- if self . panic_span . is_some ( ) {
1201
- return ;
1202
- }
1203
-
1204
- if let Some ( macro_call) = root_macro_call_first_node ( self . cx , expr) {
1205
- if is_panic ( self . cx , macro_call. def_id )
1206
- || matches ! (
1207
- self . cx. tcx. item_name( macro_call. def_id) . as_str( ) ,
1208
- "assert" | "assert_eq" | "assert_ne"
1209
- )
1210
- {
1211
- self . is_const = self . cx . tcx . hir_is_inside_const_context ( expr. hir_id ) ;
1212
- self . panic_span = Some ( macro_call. span ) ;
1213
- }
1214
- }
1215
-
1216
- // check for `unwrap` and `expect` for both `Option` and `Result`
1217
- if let Some ( arglists) = method_chain_args ( expr, & [ "unwrap" ] ) . or ( method_chain_args ( expr, & [ "expect" ] ) ) {
1218
- let receiver_ty = self . typeck_results . expr_ty ( arglists[ 0 ] . 0 ) . peel_refs ( ) ;
1219
- if is_type_diagnostic_item ( self . cx , receiver_ty, sym:: Option )
1220
- || is_type_diagnostic_item ( self . cx , receiver_ty, sym:: Result )
1221
- {
1222
- self . panic_span = Some ( expr. span ) ;
1223
- }
1224
- }
1225
-
1226
- // and check sub-expressions
1227
- intravisit:: walk_expr ( self , expr) ;
1228
- }
1229
-
1230
- // Panics in const blocks will cause compilation to fail.
1231
- fn visit_anon_const ( & mut self , _: & ' tcx AnonConst ) { }
1232
-
1233
- fn maybe_tcx ( & mut self ) -> Self :: MaybeTyCtxt {
1234
- self . cx . tcx
1235
- }
1236
- }
1237
-
1238
1150
#[ expect( clippy:: range_plus_one) ] // inclusive ranges aren't the same type
1239
1151
fn looks_like_refdef ( doc : & str , range : Range < usize > ) -> Option < Range < usize > > {
1240
1152
if range. end < range. start {
0 commit comments