fix [std_instead_of_core]: false negative for non-FQ paths #17303
fix [std_instead_of_core]: false negative for non-FQ paths #17303bushrat011899 wants to merge 2 commits into
Conversation
Only fully qualified paths were being linted before.
|
rustbot has assigned @samueltardieu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
Lintcheck changes for 1bc460f
This comment will be updated if you push new changes |
|
The large quantity of additions to |
Objective
std_instead_of_coredoes not detect uses of non-fully qualified items #11159Solution
The current version of this lint only considers fully qualified paths. For example:
This should warn that
Display,Formatter, andResultare allstd_instead_of_coreor suggest replacingstd::fmtwithcore::fmt. However, in the late lint pass it's not straightforward to determine all usages of that particularfmt, so the current best effort suggestion that can be made isstd_instead_of_alloconstd::fmt, since it is a reexport ofalloc::fmt.This PR allows the lint to work on non-fully qualified paths by checking the resolution of the base of a path and prepending it to the currently checked path. For example, when checking
fmt::Display,fmtis known not to be a crate root, so we check what its fully resolved path is, and determine it to bealloc::fmt. Concatenating the base and the path to check yields a fully qualifiedalloc::fmt::Display, and the lint can proceed with its checks, and emitalloc_instead_of_core.Note that in this example,
fmt::Displayis linted as-if it comes fromalloc::fmt::Display, yet in the user's code it actually comes fromstd::fmt::Display. This actually works perfectly, since we also know thatstd::fmtwill be linted asstd_instead_of_alloc. In effect, the non-fully qualified paths get linted as-if their base paths are already passing. If they aren't, we know they'll be linted anyway, so the user will be made aware.Notes
Please write a short comment explaining your change (or "none" for internal only changes)
changelog: allow [
std_instead_of_core] to check non-fully qualified paths.