You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/coding-guidelines/macros.rst
+19-19Lines changed: 19 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -101,22 +101,22 @@ Macros
101
101
:tags: reduce-human-error
102
102
103
103
Functions should always be preferred over macros, except when macros provide essential functionality that functions cannot, such as variadic interfaces, compile-time code generation, or syntax extensions via custom derive and attribute macros.
104
-
104
+
105
105
|
106
106
107
-
.. rationale::
107
+
.. rationale::
108
108
:id: rat_M9bp23ctkzQ7
109
109
:status: draft
110
110
111
111
Although the compiler reports both the macro expansion and its invocation site, diagnostics originating within macros can be more difficult to interpret than those from ordinary function or type definitions. Complex or deeply nested macros may obscure intent and hinder static analysis, increasing the risk of misinterpretation or overlooked errors during code review.
112
-
113
112
114
-
**Debugging Complexity**
113
+
114
+
**Debugging Complexity**
115
115
116
116
- Errors point to expanded code rather than source locations, making it difficult to trace compile-time errors back to the original macro invocation.
117
117
118
118
**Optimization**
119
-
119
+
120
120
- Macros may inhibit compiler optimizations that work better with functions.
121
121
- Macros act like ``#[inline(always)]`` functions, which can lead to code bloat.
122
122
- They don't benefit from the compiler's inlining heuristics, missing out on selective inlining where the compiler decides when inlining is beneficial.
@@ -134,7 +134,7 @@ Macros
134
134
:status: draft
135
135
136
136
Using a macro where a simple function would suffice, leads to hidden mutation:
137
-
137
+
138
138
.. code-block:: rust
139
139
140
140
macro_rules! increment_and_double {
@@ -162,7 +162,7 @@ Macros
162
162
.. code-block:: rust
163
163
164
164
fn increment_and_double(x: &mut i32) -> i32 {
165
-
*x += 1; // mutation is explicit
165
+
*x += 1; // mutation is explicit
166
166
*x * 2
167
167
}
168
168
let mut num = 5;
@@ -172,7 +172,7 @@ Macros
172
172
173
173
The function version makes the mutation and borrowing explicit in its signature, improving readability, safety, and debuggability.
174
174
175
-
175
+
176
176
177
177
.. guideline:: Shall not use Function-like Macros
178
178
:id: gui_WJlWqgIxmE8P
@@ -355,7 +355,7 @@ Macros
355
355
Attribute macros shall neither be declared nor invoked.
356
356
Prefer less powerful macros that only extend source code.
357
357
358
-
.. rationale::
358
+
.. rationale::
359
359
:id: rat_X8uCF5yx7Mpo
360
360
:status: draft
361
361
@@ -366,9 +366,9 @@ Macros
366
366
:status: draft
367
367
368
368
Explanation of code example.
369
-
369
+
370
370
.. code-block:: rust
371
-
371
+
372
372
#[tokio::main] // non-compliant
373
373
async fn main() {
374
374
@@ -379,26 +379,26 @@ Macros
379
379
:status: draft
380
380
381
381
Explanation of code example.
382
-
382
+
383
383
.. code-block:: rust
384
-
384
+
385
385
fn example_function() {
386
386
// Compliant implementation
387
387
}
388
-
388
+
389
389
.. guideline:: Do not hide unsafe blocks within macro expansions
0 commit comments