[英]clippy::missing_const_for_fn fires on constructor and getters. Is that a true positive?
I decided to use a bit of a heftier version of clippy to get myself ready for a version change.我决定使用更强大的 clippy 版本来为版本更改做好准备。 I was expecting some false positives but I'm not sure about this one.我期待一些误报,但我不确定这个。 I got the following struct, and clippy tells me that every single function should be const.我得到了以下结构,clippy 告诉我每个 function 都应该是 const。 (clippy::missing_const_for_fn) (clippy::missing_const_for_fn)
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScheduleFields {
years: Years,
days_of_week: DaysOfWeek,
months: Months,
days_of_month: DaysOfMonth,
hours: Hours,
minutes: Minutes,
seconds: Seconds,
}
impl ScheduleFields {
// Constructor
pub fn new(
seconds: Seconds,
minutes: Minutes,
hours: Hours,
days_of_month: DaysOfMonth,
months: Months,
days_of_week: DaysOfWeek,
years: Years,
) -> ScheduleFields {
ScheduleFields {
years,
days_of_week,
months,
days_of_month,
hours,
minutes,
seconds,
}
}
// Getters
pub fn years(&self) -> &Years { &self.years }
pub fn months(&self) -> &Months { &self.months }
pub fn days_of_month(&self) -> &DaysOfMonth { &self.days_of_month }
pub fn days_of_week(&self) -> &DaysOfWeek { &self.days_of_week }
pub fn hours(&self) -> &Hours { &self.hours }
pub fn minutes(&self) -> &Minutes { &self.minutes }
pub fn seconds(&self) -> &Seconds { &self.seconds }
}
missing_const_for_fn
will suggest flagging as const
anything which can be flagged thus. missing_const_for_fn
将建议将任何可以被标记的东西标记为const
。 Regardless of it being useful or not.不管它有用与否。
Here every single function can be const-ed, so clippy suggests doing that.这里每一个 function 都可以 const-ed,所以 clippy 建议这样做。 And once you've marked them as const, clippy will find that most of their callers (which currently can't be const-ed because they call non-const functions) can also be const-ed.一旦你将它们标记为 const,clippy 会发现它们的大多数调用者(目前不能被 const-ed,因为它们调用非 const 函数)也可以被 const-ed。
Personally , I don't think this lint is useful or should be used: much like Copy
, const
is a very strong and restrictive API promise.就个人而言,我不认为这种 lint 有用或不应该使用:就像Copy
一样, const
是一个非常强大和限制性的 API promise。 It should only be enabled on a case-per-case basis after careful consideration.仅应在仔细考虑后根据具体情况启用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.