Skip to content

Commit 4ef57d3

Browse files
committed
Auto merge of #12558 - y21:issue9150, r=xFrednet
[`let_and_return`]: avoid linting when code between last stmt and return expr is cfg'd out Fixes #9150 This moves `span_contains_cfg` to utils and starts using it in `let_and_return` as well. changelog: [`let_and_return`]: avoid linting when code between the last statement and the final return expression is `#[cfg]`ed out
2 parents c3948d1 + 9e82ad8 commit 4ef57d3

File tree

5 files changed

+50
-31
lines changed

5 files changed

+50
-31
lines changed

clippy_lints/src/matches/mod.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ mod try_err;
2525
mod wild_in_or_pats;
2626

2727
use clippy_config::msrvs::{self, Msrv};
28-
use clippy_utils::source::{snippet_opt, walk_span_to_context};
29-
use clippy_utils::{higher, in_constant, is_direct_expn_of, is_span_match, tokenize_with_text};
28+
use clippy_utils::source::walk_span_to_context;
29+
use clippy_utils::{higher, in_constant, is_direct_expn_of, is_span_match, span_contains_cfg};
3030
use rustc_hir::{Arm, Expr, ExprKind, Local, MatchSource, Pat};
31-
use rustc_lexer::TokenKind;
3231
use rustc_lint::{LateContext, LateLintPass, LintContext};
3332
use rustc_middle::lint::in_external_macro;
3433
use rustc_session::impl_lint_pass;
35-
use rustc_span::{Span, SpanData, SyntaxContext};
34+
use rustc_span::{SpanData, SyntaxContext};
3635

3736
declare_clippy_lint! {
3837
/// ### What it does
@@ -1196,28 +1195,3 @@ fn contains_cfg_arm(cx: &LateContext<'_>, e: &Expr<'_>, scrutinee: &Expr<'_>, ar
11961195
Err(()) => true,
11971196
}
11981197
}
1199-
1200-
/// Checks if the given span contains a `#[cfg(..)]` attribute
1201-
fn span_contains_cfg(cx: &LateContext<'_>, s: Span) -> bool {
1202-
let Some(snip) = snippet_opt(cx, s) else {
1203-
// Assume true. This would require either an invalid span, or one which crosses file boundaries.
1204-
return true;
1205-
};
1206-
let mut iter = tokenize_with_text(&snip);
1207-
1208-
// Search for the token sequence [`#`, `[`, `cfg`]
1209-
while iter.any(|(t, _)| matches!(t, TokenKind::Pound)) {
1210-
let mut iter = iter.by_ref().skip_while(|(t, _)| {
1211-
matches!(
1212-
t,
1213-
TokenKind::Whitespace | TokenKind::LineComment { .. } | TokenKind::BlockComment { .. }
1214-
)
1215-
});
1216-
if matches!(iter.next(), Some((TokenKind::OpenBracket, _)))
1217-
&& matches!(iter.next(), Some((TokenKind::Ident, "cfg")))
1218-
{
1219-
return true;
1220-
}
1221-
}
1222-
false
1223-
}

clippy_lints/src/returns.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::source::{snippet_opt, snippet_with_context};
33
use clippy_utils::sugg::has_enclosing_paren;
44
use clippy_utils::visitors::{for_each_expr_with_closures, Descend};
55
use clippy_utils::{
6-
fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor, path_res, path_to_local_id,
6+
fn_def_id, is_from_proc_macro, is_inside_let_else, is_res_lang_ctor, path_res, path_to_local_id, span_contains_cfg,
77
span_find_starting_semi,
88
};
99
use core::ops::ControlFlow;
@@ -232,6 +232,7 @@ impl<'tcx> LateLintPass<'tcx> for Return {
232232
&& !in_external_macro(cx.sess(), initexpr.span)
233233
&& !in_external_macro(cx.sess(), retexpr.span)
234234
&& !local.span.from_expansion()
235+
&& !span_contains_cfg(cx, stmt.span.between(retexpr.span))
235236
{
236237
span_lint_hir_and_then(
237238
cx,

clippy_utils/src/attrs.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
use rustc_ast::{ast, attr};
22
use rustc_errors::Applicability;
3+
use rustc_lexer::TokenKind;
4+
use rustc_lint::LateContext;
35
use rustc_middle::ty::{AdtDef, TyCtxt};
46
use rustc_session::Session;
5-
use rustc_span::sym;
7+
use rustc_span::{sym, Span};
68
use std::str::FromStr;
79

10+
use crate::source::snippet_opt;
11+
use crate::tokenize_with_text;
12+
813
/// Deprecation status of attributes known by Clippy.
914
pub enum DeprecationStatus {
1015
/// Attribute is deprecated
@@ -171,3 +176,28 @@ pub fn has_non_exhaustive_attr(tcx: TyCtxt<'_>, adt: AdtDef<'_>) -> bool {
171176
.all_fields()
172177
.any(|field_def| tcx.has_attr(field_def.did, sym::non_exhaustive))
173178
}
179+
180+
/// Checks if the given span contains a `#[cfg(..)]` attribute
181+
pub fn span_contains_cfg(cx: &LateContext<'_>, s: Span) -> bool {
182+
let Some(snip) = snippet_opt(cx, s) else {
183+
// Assume true. This would require either an invalid span, or one which crosses file boundaries.
184+
return true;
185+
};
186+
let mut iter = tokenize_with_text(&snip);
187+
188+
// Search for the token sequence [`#`, `[`, `cfg`]
189+
while iter.any(|(t, _)| matches!(t, TokenKind::Pound)) {
190+
let mut iter = iter.by_ref().skip_while(|(t, _)| {
191+
matches!(
192+
t,
193+
TokenKind::Whitespace | TokenKind::LineComment { .. } | TokenKind::BlockComment { .. }
194+
)
195+
});
196+
if matches!(iter.next(), Some((TokenKind::OpenBracket, _)))
197+
&& matches!(iter.next(), Some((TokenKind::Ident, "cfg")))
198+
{
199+
return true;
200+
}
201+
}
202+
false
203+
}

tests/ui/let_and_return.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,11 @@ fn_in_macro!({
203203
return 1;
204204
});
205205

206+
fn issue9150() -> usize {
207+
let x = 1;
208+
#[cfg(any())]
209+
panic!("can't see me");
210+
x
211+
}
212+
206213
fn main() {}

tests/ui/let_and_return.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,11 @@ fn_in_macro!({
203203
return 1;
204204
});
205205

206+
fn issue9150() -> usize {
207+
let x = 1;
208+
#[cfg(any())]
209+
panic!("can't see me");
210+
x
211+
}
212+
206213
fn main() {}

0 commit comments

Comments
 (0)