Skip to content

Merge Cfg::render_long_html and Cfg::render_long_plain methods common code #141770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/librustdoc/clean/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,33 +169,36 @@ impl Cfg {
msg
}

/// Renders the configuration for long display, as a long HTML description.
pub(crate) fn render_long_html(&self) -> String {
fn render_long_inner(&self, format: Format) -> String {
let on = if self.omit_preposition() {
""
" "
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this a bit because the generated whitespace characters were invalid in render_long_plain with the previous code (two whitespace characters after Available for this case).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little confused about why this had to be changed. It seems like every branch of this if-else has a leading space. But it doesn't matter.

} else if self.should_use_with_in_description() {
"with "
" with "
} else {
"on "
" on "
};

let mut msg = format!("Available {on}<strong>{}</strong>", Display(self, Format::LongHtml));
let mut msg = if matches!(format, Format::LongHtml) {
format!("Available{on}<strong>{}</strong>", Display(self, format))
} else {
format!("Available{on}{}", Display(self, format))
};
if self.should_append_only_to_description() {
msg.push_str(" only");
}
msg
}

/// Renders the configuration for long display, as a long HTML description.
pub(crate) fn render_long_html(&self) -> String {
let mut msg = self.render_long_inner(Format::LongHtml);
msg.push('.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't matter for this PR since it's preexisting, but any idea why we end with a . for the html but not for the plain?

msg
}

/// Renders the configuration for long display, as a long plain text description.
pub(crate) fn render_long_plain(&self) -> String {
let on = if self.should_use_with_in_description() { "with" } else { "on" };

let mut msg = format!("Available {on} {}", Display(self, Format::LongPlain));
if self.should_append_only_to_description() {
msg.push_str(" only");
}
msg
self.render_long_inner(Format::LongPlain)
}

fn should_capitalize_first_letter(&self) -> bool {
Expand Down
10 changes: 7 additions & 3 deletions tests/rustdoc/cfg-bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

// regression test for https://github.com/rust-lang/rust/issues/138112

//@ has 'foo/fn.foo.html' '//div[@class="stab portability"]' 'Available nowhere'
//@ has 'foo/index.html'
//@ has - '//*[@class="stab portability"]/@title' 'Available nowhere'

//@ count 'foo/fn.foo.html' '//*[@class="stab portability"]' 1
//@ has 'foo/fn.foo.html' '//*[@class="stab portability"]' 'Available nowhere'
#[doc(cfg(false))]
pub fn foo() {}

// a cfg(true) will simply be ommited, as it is the same as no cfg.
//@ !has 'foo/fn.bar.html' '//div[@class="stab portability"]' ''
// a cfg(true) will simply be omitted, as it is the same as no cfg.
//@ count 'foo/fn.bar.html' '//*[@class="stab portability"]' 0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should maybe restrain usage of !has as it's so easily outdated...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably would be a good idea to just mention it in the docs we have now, "consider using count <...> 0 instead of !has

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not much better though. We need to find a way to prevent it from ever rotting.

#[doc(cfg(true))]
pub fn bar() {}
Loading