Skip to content

Commit c6c9c7f

Browse files
authored
Include constness in impl blocks (#4215)
Closes #4084
1 parent 6a861fb commit c6c9c7f

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

rustfmt-core/rustfmt-lib/src/formatting/items.rs

+2
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ fn format_impl_ref_and_type(
905905
unsafety,
906906
polarity,
907907
defaultness,
908+
constness,
908909
ref generics,
909910
of_trait: ref trait_ref,
910911
ref self_ty,
@@ -920,6 +921,7 @@ fn format_impl_ref_and_type(
920921
let shape = Shape::indented(offset + last_line_width(&result), context.config);
921922
let generics_str = rewrite_generics(context, "impl", generics, shape)?;
922923
result.push_str(&generics_str);
924+
result.push_str(format_constness_right(constness));
923925

924926
let polarity_str = match polarity {
925927
ast::ImplPolarity::Negative(_) => "!",

rustfmt-core/rustfmt-lib/src/formatting/utils.rs

+8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ pub(crate) fn format_constness(constness: ast::Const) -> &'static str {
102102
}
103103
}
104104

105+
#[inline]
106+
pub(crate) fn format_constness_right(constness: ast::Const) -> &'static str {
107+
match constness {
108+
ast::Const::Yes(..) => " const",
109+
ast::Const::No => "",
110+
}
111+
}
112+
105113
#[inline]
106114
pub(crate) fn format_defaultness(defaultness: ast::Defaultness) -> &'static str {
107115
match defaultness {

rustfmt-core/rustfmt-lib/tests/source/impls.rs

+8
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,11 @@ impl<'a, 'b, 'c> SomeThing<Something> for (&'a mut SomethingLong, &'b mut Someth
160160

161161
// #2746
162162
impl<'seq1, 'seq2, 'body, 'scope, Channel> Adc12< Dual, MasterRunningDma<'seq1, 'body, 'scope, Channel>, SlaveRunningDma<'seq2, 'body, 'scope>, > where Channel: DmaChannel, {}
163+
164+
// #4084
165+
impl const std::default::Default for Struct {
166+
#[inline]
167+
fn default() -> Self {
168+
Self { f: 12.5 }
169+
}
170+
}

rustfmt-core/rustfmt-lib/tests/target/impls.rs

+8
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,11 @@ where
234234
Channel: DmaChannel,
235235
{
236236
}
237+
238+
// #4084
239+
impl const std::default::Default for Struct {
240+
#[inline]
241+
fn default() -> Self {
242+
Self { f: 12.5 }
243+
}
244+
}

0 commit comments

Comments
 (0)