Skip to content

Commit 52c902f

Browse files
committed
fix rust-lang#105061, Fix unused_parens issue for higher ranked function pointers
1 parent 3b38adf commit 52c902f

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,20 @@ impl EarlyLintPass for UnusedParens {
10381038
if let ast::TyKind::Paren(r) = &ty.kind {
10391039
match &r.kind {
10401040
ast::TyKind::TraitObject(..) => {}
1041-
ast::TyKind::BareFn(b) if b.generic_params.len() > 0 => {}
1041+
ast::TyKind::BareFn(b) => {
1042+
let spans = if let Some(r) = r.span.find_ancestor_inside(ty.span) {
1043+
Some((ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
1044+
} else {
1045+
None
1046+
};
1047+
let sm = cx.sess().source_map();
1048+
if let Some((_, r)) = spans &&
1049+
b.generic_params.len() > 0 &&
1050+
sm.span_followed_by(r, ":") {
1051+
return;
1052+
}
1053+
self.emit_unused_delims(cx, ty.span, spans, "type", (false, false));
1054+
}
10421055
ast::TyKind::ImplTrait(_, bounds) if bounds.len() > 1 => {}
10431056
ast::TyKind::Array(_, len) => {
10441057
self.check_unused_delims_expr(

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,8 +1158,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
11581158
// where a brace being opened means a block is being started. Look
11591159
// ahead for the next text to see if `span` is followed by a `{`.
11601160
let sm = self.r.session.source_map();
1161-
let sp = sm.span_look_ahead(span, None, Some(50));
1162-
let followed_by_brace = matches!(sm.span_to_snippet(sp), Ok(ref snippet) if snippet == "{");
1161+
let followed_by_brace = sm.span_followed_by(span, "{");
11631162
// In case this could be a struct literal that needs to be surrounded
11641163
// by parentheses, find the appropriate span.
11651164
let closing_span = sm.span_look_ahead(span, Some("}"), Some(50));

compiler/rustc_span/src/source_map.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,14 @@ impl SourceMap {
962962
sp
963963
}
964964

965+
/// Check whether a `span` is followed by a specific string
966+
pub fn span_followed_by(&self, span: Span, expect: &str) -> bool {
967+
if let Ok(source) = self.span_to_next_source(span) {
968+
return source.trim_start().starts_with(expect);
969+
}
970+
false
971+
}
972+
965973
/// Finds the width of the character, either before or after the end of provided span,
966974
/// depending on the `forwards` parameter.
967975
fn find_width_of_character_at_span(&self, sp: Span, forwards: bool) -> u32 {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![warn(unused)]
2+
#![deny(warnings)]
3+
4+
struct Inv<'a>(&'a mut &'a ());
5+
6+
trait Trait {}
7+
impl Trait for (for<'a> fn(Inv<'a>),) {}
8+
9+
10+
fn with_bound()
11+
where
12+
((for<'a> fn(Inv<'a>)),): Trait, //~ ERROR unnecessary parentheses around type
13+
{}
14+
15+
fn main() {
16+
with_bound();
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: unnecessary parentheses around type
2+
--> $DIR/issue-105061.rs:12:6
3+
|
4+
LL | ((for<'a> fn(Inv<'a>)),): Trait,
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-105061.rs:2:9
9+
|
10+
LL | #![deny(warnings)]
11+
| ^^^^^^^^
12+
= note: `#[deny(unused_parens)]` implied by `#[deny(warnings)]`
13+
help: remove these parentheses
14+
|
15+
LL - ((for<'a> fn(Inv<'a>)),): Trait,
16+
LL + (for<'a> fn(Inv<'a>),): Trait,
17+
|
18+
19+
error: aborting due to previous error
20+

0 commit comments

Comments
 (0)