{"record":{"id":"b35c926447651cf7","repo":"sxyazi/yazi","slug":"not-an-integer","errorCode":null,"errorMessage":"not an integer","messagePattern":"not an integer","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"yazi-shared/src/data/macros.rs","lineNumber":9,"sourceCode":"use anyhow::bail;\n\npub(crate) fn float_to_i64<T>(value: T) -> anyhow::Result<i64>\nwhere\n\tT: Into<f64>,\n{\n\tlet value = value.into();\n\tif !value.is_finite() || value.fract() != 0.0 {\n\t\tbail!(\"not an integer\");\n\t}\n\n\tlet integer = value as i64;\n\tif integer as f64 != value {\n\t\tbail!(\"not an integer\");\n\t}\n\n\tOk(integer)\n}\n\nmacro_rules! ref_or_owned {\n\t(ref, $value:expr) => {\n\t\t&$value\n\t};\n\t(owned, $value:expr) => {\n\t\t$value\n\t};\n}","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/sxyazi/yazi/blob/5f901b886b14de1f17460b6e52e9de5d67f8aba9/yazi-shared/src/data/macros.rs#L1-L27","documentation":"Raised inside `float_to_i64` in yazi-shared/src/data/macros.rs when a floating-point value cannot be represented losslessly as an i64. It bails either when the value is non-finite (NaN/infinity) or has a fractional part, or when the `as i64` cast round-trips back to a different float (overflow beyond i64 range).","triggerScenarios":"Converting `Data::Number(f32/f64)` via `impl_into_integer!` where the float is e.g. 3.5, NaN, or 1e300.","commonSituations":"Lua/plugin code passing a fractional number where an integer is required (e.g. an index or count), or calculations producing huge values that overflow i64.","solutions":["Validate the number before conversion: finite and `fract() == 0.0`.","If fractional input is legitimate, round or truncate explicitly (`value.round() as i64`) instead of relying on TryFrom.","Clamp/saturate extreme values before casting to avoid range overflow."],"exampleFix":"// before\nlet n: i64 = Data::Number(3.5f64).try_into()?;\n// after\nlet f = 3.5f64;\nlet n: i64 = if f.is_finite() && f.fract() != 0.0 { f.round() as i64 } else { f as i64 };","handlingStrategy":"validation","validationCode":"fn is_lossless_i64(f: f64) -> bool {\n    f.is_finite() && f.fract() == 0.0 && f >= i64::MIN as f64 && f <= i64::MAX as f64\n}","typeGuard":null,"tryCatchPattern":"match Data::Number(f).try_into() as Result<i64, _> {\n    Ok(n) => use_int(n),\n    Err(e) => log::warn!(\"{f} is not an integer: {e}\"),\n}","preventionTips":["Validate floats with is_finite() and fract() == 0.0 before integer conversion.","Round explicitly at the source when fractional values are acceptable.","Avoid floating-point arithmetic for counts/indices; keep integers as integers."],"tags":["rust","numeric","float-to-integer"],"backgroundTag":"float-to-integer-conversion","analyzedSha":"5f901b886b14de1f17460b6e52e9de5d67f8aba9","analyzedAt":"2026-09-02T18:38:25.566Z","contentChangedAt":"2026-09-02T18:38:25.566Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}