Repository navigation
Commit c683145
authored
Rollup merge of #163239 - chenyukang:yukang-fix-cfg-tail-expression-suggestion, r=estebank
Fix applicability level for cfg suggestion
Mark the semicolon, block-wrapping, and if cfg! suggestions for attribute-bearing expressions as `MaybeIncorrect`.
For example:
```rust
fn native_file_attributes(metadata: &std::fs::Metadata) -> u32 {
#[cfg(unix)]
std::os::unix::fs::MetadataExt::mode(metadata)
#[cfg(windows)]
std::os::windows::fs::MetadataExt::file_attributes(metadata)
}
fn main() -> std::io::Result<()> {
let metadata = std::fs::metadata(std::env::current_exe()?)?;
println!(
"native file attributes: {:#x}",
native_file_attributes(&metadata)
);
Ok(())
}
```
Adding ; discards the return value on Unix.
Replacing #[cfg] with if cfg! also fails because disabled branches still undergo name resolution and type checking.
Wrapping the expression in a block works here, but still can fail when multiple cfg branches are enabled and the first block returns a non-unit value.
For example:
```rust
fn value() -> u8 {
#[cfg(all())]
1
#[cfg(all())]
2
}
```
r? @estebank2 files changed
Lines changed: 8 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1656 | 1656 | | |
1657 | 1657 | | |
1658 | 1658 | | |
1659 | | - | |
1660 | | - | |
| 1659 | + | |
| 1660 | + | |
1661 | 1661 | | |
1662 | 1662 | | |
1663 | 1663 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
439 | 439 | | |
440 | 440 | | |
441 | 441 | | |
442 | | - | |
| 442 | + | |
443 | 443 | | |
444 | 444 | | |
445 | 445 | | |
| |||
723 | 723 | | |
724 | 724 | | |
725 | 725 | | |
726 | | - | |
| 726 | + | |
| 727 | + | |
727 | 728 | | |
728 | 729 | | |
729 | 730 | | |
| |||
755 | 756 | | |
756 | 757 | | |
757 | 758 | | |
758 | | - | |
| 759 | + | |
759 | 760 | | |
760 | 761 | | |
761 | 762 | | |
| |||
811 | 812 | | |
812 | 813 | | |
813 | 814 | | |
814 | | - | |
| 815 | + | |
| 816 | + | |
815 | 817 | | |
816 | 818 | | |
817 | 819 | | |
| |||
0 commit comments