mirror of
https://github.com/mfocko/blog.git
synced 2024-11-21 20:43:48 +01:00
cpp(placeholders): fix the static analysis
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
e881234412
commit
4ab2615a05
2 changed files with 23 additions and 6 deletions
|
@ -203,3 +203,20 @@ void unreachable(std::format_string<Args...> fmt, Args&&... args) {
|
||||||
Final source code: [`placeholders.hpp`](pathname:///files/cpp/exceptions-and-raii/placeholders/placeholders.hpp)
|
Final source code: [`placeholders.hpp`](pathname:///files/cpp/exceptions-and-raii/placeholders/placeholders.hpp)
|
||||||
|
|
||||||
:::
|
:::
|
||||||
|
|
||||||
|
## Post-mortem
|
||||||
|
|
||||||
|
One of the things, I've forgotten about, is the fact that static analysis of
|
||||||
|
your code has no way to know those helper functions we've created as shortcuts
|
||||||
|
don't return and just throw the exception right away. Therefore we need to mark
|
||||||
|
them with `[[noreturn]]` to let the static analysis know that we **never**
|
||||||
|
return from such functions. For example:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
[[noreturn]] void unreachable() { throw _unreachable(); }
|
||||||
|
|
||||||
|
template <class... Args>
|
||||||
|
[[noreturn]] void unreachable(std::format_string<Args...> fmt, Args&&... args) {
|
||||||
|
throw _unreachable(std::format(fmt, args...));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
|
@ -14,7 +14,7 @@ class _todo : public std::exception {
|
||||||
/**
|
/**
|
||||||
* @brief Indicates unfinished code.
|
* @brief Indicates unfinished code.
|
||||||
*/
|
*/
|
||||||
void todo() { throw _todo(); }
|
[[noreturn]] void todo() { throw _todo(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Indicates unfinished code.
|
* @brief Indicates unfinished code.
|
||||||
|
@ -22,7 +22,7 @@ void todo() { throw _todo(); }
|
||||||
* @param args arguments to be formatted
|
* @param args arguments to be formatted
|
||||||
*/
|
*/
|
||||||
template <class... Args>
|
template <class... Args>
|
||||||
void todo(std::format_string<Args...> fmt, Args&&... args) {
|
[[noreturn]] void todo(std::format_string<Args...> fmt, Args&&... args) {
|
||||||
throw _todo(std::format(fmt, args...));
|
throw _todo(std::format(fmt, args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ class _unimplemented : public std::exception {
|
||||||
/**
|
/**
|
||||||
* @brief Indicates unimplemented code by throwing with a message of “not implemented”.
|
* @brief Indicates unimplemented code by throwing with a message of “not implemented”.
|
||||||
*/
|
*/
|
||||||
void unimplemented() { throw _unimplemented(); }
|
[[noreturn]] void unimplemented() { throw _unimplemented(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Indicates unimplemented code by throwing with a message of “not implemented”.
|
* @brief Indicates unimplemented code by throwing with a message of “not implemented”.
|
||||||
|
@ -47,7 +47,7 @@ void unimplemented() { throw _unimplemented(); }
|
||||||
* @param args arguments to be formatted
|
* @param args arguments to be formatted
|
||||||
*/
|
*/
|
||||||
template <class... Args>
|
template <class... Args>
|
||||||
void unimplemented(std::format_string<Args...> fmt, Args&&... args) {
|
[[noreturn]] void unimplemented(std::format_string<Args...> fmt, Args&&... args) {
|
||||||
throw _unimplemented(std::format(fmt, args...));
|
throw _unimplemented(std::format(fmt, args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ class _unreachable : public std::exception {
|
||||||
/**
|
/**
|
||||||
* @brief Indicates unreachable code.
|
* @brief Indicates unreachable code.
|
||||||
*/
|
*/
|
||||||
void unreachable() { throw _unreachable(); }
|
[[noreturn]] void unreachable() { throw _unreachable(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Indicates unreachable code.
|
* @brief Indicates unreachable code.
|
||||||
|
@ -72,6 +72,6 @@ void unreachable() { throw _unreachable(); }
|
||||||
* @param args arguments to be formatted
|
* @param args arguments to be formatted
|
||||||
*/
|
*/
|
||||||
template <class... Args>
|
template <class... Args>
|
||||||
void unreachable(std::format_string<Args...> fmt, Args&&... args) {
|
[[noreturn]] void unreachable(std::format_string<Args...> fmt, Args&&... args) {
|
||||||
throw _unreachable(std::format(fmt, args...));
|
throw _unreachable(std::format(fmt, args...));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue