Add static check to ensure std::forward and mozilla::Forward are only used on forwarding references

NEW
Unassigned

Status

()

Core
Rewriting and Analysis
--
enhancement
10 months ago
10 months ago

People

(Reporter: gerald, Unassigned)

Tracking

49 Branch
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

10 months ago
Following the issue found in bug 1332779 (Forward being used on a non-reference type), it would be nice to have an automated check to catch such misuses.

The expected use of mozila::Forward and std::forward is:
> template <typename T>
> void foo(T&& a)
> {
>   expr(Forward<T>(a));
> }
I.e.: The argument of Forward is a variable declared exactly as 'T&&' in the function declaration, where 'T' is given to Forward as the template argument, and 'T' is a function template parameter. (Hopefully that's complete and correct.)

The above use naturally extends to parameter packs, so they should be allowed as well:
> template <typename... Ts>
> void foo(Ts&&... as)
> {
>   expr(Forward<Ts>(as)...);
> }
The expansion ellipsis applied to Forward may be a few expression levels out, so this will be "interesting" to correctly match!
E.g.: `{ bar(baz(Forward<Ts>(as)))... }`
Maybe we should just not worry about the '...' and accept type packs and corresponding argument packs, assuming compilers will make sure the '...' is at the correct spot?


Note that 'auto&&' is also a forwarding reference (except when deduced from a brace-enclosed initializer list), though less commonly-used -- I've only just (re-)learned about it while writing this bug! We should determine if Forward can be used with it, to know whether we should accept it in our check.
(Reporter)

Comment 1

10 months ago
(typo in bug title)
Summary: Add static check to ensure std::forward and mozilla::Forward are only used on a forwarding references → Add static check to ensure std::forward and mozilla::Forward are only used on forwarding references
You need to log in before you can comment on or make changes to this bug.