Emit warning when 'String' is used as a parameter.

The warning is only emitted when Rocket is compiled in debug.
This commit is contained in:
Sergio Benitez 2023-04-10 12:47:09 -07:00
parent a82508b403
commit e66db09a6c
1 changed files with 7 additions and 1 deletions

View File

@ -195,9 +195,15 @@ impl<'a> FromParam<'a> for &'a str {
impl<'a> FromParam<'a> for String {
type Error = std::convert::Infallible;
#[track_caller]
#[inline(always)]
fn from_param(param: &'a str) -> Result<String, Self::Error> {
// TODO: Tell the user they're being inefficient?
#[cfg(debug_assertions)] {
let loc = std::panic::Location::caller();
warn_!("Note: Using `String` as a parameter type is inefficient. Use `&str` instead.");
info_!("`String` is used a parameter guard in {}:{}.", loc.file(), loc.line());
}
Ok(param.to_string())
}
}