问题描述
我决定使用一些更强大的 clippy 版本来让自己为版本更改做好准备。我期待一些误报,但我不确定这个。我得到了以下结构体,clippy 告诉我每个函数都应该是 const。 (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,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
将建议标记为 const
任何可以被标记的内容。不管有用与否。
这里的每个函数都可以被常量化,所以 clippy 建议这样做。一旦你将它们标记为 const,clippy 会发现他们的大多数调用者(目前不能被 const-ed,因为他们调用非常量函数)也可以被 const-ed。
就个人而言,我不认为这个 lint 有用或不应该使用:很像 Copy
,const
是一个非常强大和限制性的 API 承诺。只有在仔细考虑后才能逐案启用它。