rust-analyzer/crates/hir/src
bors 77e1969c15 Auto merge of #18052 - Coekjan:fix-inline-const, r=Veykril
fix: Fix `inline_const_as_literal` error when the number >= 10

## Description

### The Bug

This PR fixes a small bug in the IDE assistence (`inline_const_as_literal`). When the being-inlined constant is a number and it is greater than or equal to 10, the assistence inserts unexpected string `(0x...)` after the number itself. A simple example is followed:

Current `inline_const_as_literal` changes

```rs
const A: usize = 16;

fn f() -> usize {
    A  // inline the constant
}
```

into

```rs
const A: usize = 16;

fn f() -> usize {
    16 (0x10)
}
```

The bug originates from #14925 & #15306 . #14925 added some unittests, but it just tested the number-inlining behavior when the number is `0`.

50882fbfa2/crates/ide-assists/src/handlers/inline_const_as_literal.rs (L124-L138)

And #15306 modified the behavior of `Const::render_eval` and added the `(0x...)` part after the number (if the number >= `10`). Because of insufficient unittests in #14925, changes about `Const::render_eval` in #15306 introduced this bug with no CI failure.

### The Fix

I think `Const::render_eval` is intended for user-facing value displaying (e.g. hover) and not designed for `inline_const_as_literal`. To fix the bug, I defined a new function named `Const::eval`, which evaluates the value itself faithfully and simply and does nothing else.

## Thanks

Thanks `@roife` for your kind help. Your guidance helped me better understand the code.
2024-09-11 10:48:32 +00:00
..
semantics Give InlineAsmOperand a HIR representation 2024-09-05 12:40:48 +02:00
term_search Properly account for editions in names 2024-08-16 16:46:24 +03:00
attrs.rs Properly account for editions in names 2024-08-16 16:46:24 +03:00
db.rs Update per_query_memory_usage query listing 2024-04-17 18:44:50 +02:00
diagnostics.rs feat: Implement cast typechecks 2024-09-03 04:11:36 +09:00
display.rs Properly account for editions in names 2024-08-16 16:46:24 +03:00
from_id.rs Enum variants are not generic def ids 2024-06-24 10:07:31 +02:00
has_source.rs Support more IDE features for asm operands 2024-09-05 13:19:02 +02:00
lib.rs Auto merge of #18052 - Coekjan:fix-inline-const, r=Veykril 2024-09-11 10:48:32 +00:00
semantics.rs asm! parsing and lowering fixes 2024-09-05 15:08:16 +02:00
source_analyzer.rs asm! parsing and lowering fixes 2024-09-05 15:08:16 +02:00
symbols.rs Properly account for editions in names 2024-08-16 16:46:24 +03:00
term_search.rs Increase search depth to account for more granual steps 2024-06-22 15:06:07 +03:00