Skip to content

Commit 6ea8e79

Browse files
committed
Refactor trait signature lookup and param parsing
1 parent 895f817 commit 6ea8e79

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,11 +1698,14 @@ fn compare_number_of_method_arguments<'tcx>(
16981698
let impl_number_args = impl_m_fty.skip_binder().inputs().skip_binder().len();
16991699

17001700
if trait_number_args != impl_number_args {
1701-
let trait_span = trait_m
1702-
.def_id
1703-
.as_local()
1704-
.and_then(|def_id| {
1705-
let (trait_m_sig, _) = &tcx.hir_expect_trait_item(def_id).expect_fn();
1701+
let trait_sig = trait_m.def_id.as_local().and_then(|def_id| {
1702+
let (trait_m_sig, _) = tcx.hir_expect_trait_item(def_id).expect_fn();
1703+
Some((def_id, trait_m_sig))
1704+
});
1705+
1706+
let trait_span = trait_sig
1707+
.as_ref()
1708+
.and_then(|(_, trait_m_sig)| {
17061709
let pos = trait_number_args.saturating_sub(1);
17071710
trait_m_sig.decl.inputs.get(pos).map(|arg| {
17081711
if pos == 0 {
@@ -1762,15 +1765,16 @@ fn compare_number_of_method_arguments<'tcx>(
17621765
);
17631766

17641767
let sm = tcx.sess.source_map();
1765-
let find_param_bounds = |snippet: &str| -> Option<(usize, usize)> {
1768+
1769+
fn find_param_bounds(snippet: &str) -> Option<(usize, usize)> {
17661770
let start = snippet.find('(')?;
17671771
let mut depth = 1usize;
17681772
let bytes = snippet.as_bytes();
17691773
let mut i = start + 1;
17701774
while i < bytes.len() {
1771-
match bytes[i] as char {
1772-
'(' => depth += 1,
1773-
')' => {
1775+
match bytes[i] {
1776+
b'(' => depth += 1,
1777+
b')' => {
17741778
depth -= 1;
17751779
if depth == 0 {
17761780
return Some((start + 1, i));
@@ -1781,7 +1785,7 @@ fn compare_number_of_method_arguments<'tcx>(
17811785
i += 1;
17821786
}
17831787
None
1784-
};
1788+
}
17851789

17861790
let impl_inputs_span = (!impl_m_sig.span.is_dummy())
17871791
.then(|| {
@@ -1795,11 +1799,8 @@ fn compare_number_of_method_arguments<'tcx>(
17951799
})
17961800
.flatten();
17971801

1798-
let suggestion = trait_m
1799-
.def_id
1800-
.as_local()
1801-
.and_then(|def_id| {
1802-
let (trait_sig, _) = tcx.hir_expect_trait_item(def_id).expect_fn();
1802+
let suggestion = trait_sig
1803+
.and_then(|(_, trait_sig)| {
18031804
sm.span_to_snippet(trait_sig.span).ok().and_then(|snippet| {
18041805
find_param_bounds(&snippet)
18051806
.map(|(lo_rel, hi_rel)| snippet[lo_rel..hi_rel].to_string())

0 commit comments

Comments
 (0)